1

I'm trying to create a date from array. Is there any function that could accomplish this? What i know is i can access to fields and create date from that.

'endDate' => array(
        'month' => '05',
        'day' => '30',
        'year' => '2017',
        'hour' => '10',
        'min' => '48',
        'meridian' => 'pm'
    )
3

3 Answers 3

2

I used DateTime::createFromFormat and vsprintf to construct a time string from the array, and came up with this:

<?php
$endDate = array(
    'month' => '05',
    'day' => '30',
    'year' => '2017',
    'hour' => '10',
    'min' => '48',
    'meridian' => 'pm'
);

$date = DateTime::createFromFormat('d/m/Y h:i a', vsprintf('%s/%s/%s %s:%s %s', [
    $endDate['day'],
    $endDate['month'],
    $endDate['year'],
    $endDate['hour'],
    $endDate['min'],
    $endDate['meridian'],
]));

echo $date->format('d/m/Y h:i a');

Outputs (and you can tweak the format:

30/05/2017 10:48 pm

Actually you probably don't even need to use DateTime but I would suggest you do as you can then further manipulate it (add time intervals or change the format), you could probably just do:

printf('%s/%s/%s %s:%s %s', [
    $endDate['day'],
    $endDate['month'],
    $endDate['year'],
    $endDate['hour'],
    $endDate['min'],
    $endDate['meridian'],
]

Demo: https://eval.in/778600

Sign up to request clarification or add additional context in comments.

1 Comment

is there any way to convert the result to miliseconds. result: '18/04/2017 04:48 pm'
0

You can assemble your value into a value date string then create a new instance of DateTime of call strtotime.

$endtime = [
    'month' => '05',
    'day' => '30',
    'year' => '2017',
    'hour' => '10',
    'min' => '48',
    'meridian' => 'pm'
];
$datetime = DateTime::createFromFormat("Y-m-d h:i A", "{$endtime['year']}-{$endtime['month']}-{$endtime['day']} {$endtime['hour']}:{$endtime['min']} {$endtime['meridian']}");
$time = strtotime("{$endtime['year']}-{$endtime['month']}-{$endtime['day']} {$endtime['hour']}:{$endtime['min']} {$endtime['meridian']}");

Comments

0

There's no API function, which can accomplish that. You could implement your own method, in which should look like this:

/**
 * Convert given array to DateTimeObject
 * @param $dateArr array containing 'day', 'month', 'year', 'hour', 'min', 'meridian'
 * @return DateTime object
 *
 * 'day' and 'month' must always be 2 numbers
 * 'year' must always be 4 numbers
 * 'hour' must always be 2 numbers (01-12)
 * 'min' must always be 2 numbers (00-59)
 * 'meridian' must always be 2 letters ('am' or 'pm')
 */
function createEndDate($dateArr) {
    return DateTime::createFromFormat('dmYhia',
        $dateArr['day'] . 
        $dateArr['month'] . 
        $dateArr['year'] .
        $dateArr['hour'] .
        $dateArr['min'] .
        strtolower($dateArr['meridian']));
}

Code example:

$yourArr => array(
    'month' => '05',
    'day' => '30',
    'year' => '2017',
    'hour' => '10',
    'min' => '48',
    'meridian' => 'pm'
)

$endDate = createEndDate($yourArr);
// see http://php.net/manual/de/class.datetime.php for docs
echo $endDate->format('u');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.