Roch
Roch

Reputation: 22041

Strtotime with European date format

I'm trying to use strtotime to convert the following date:

07/09/2009 17:01:27

It's the Europe/London timezone format for 7th of September

The function is inverting the month and the days, is there any workaround? thanks

Upvotes: 4

Views: 22501

Answers (6)

Daniel Paul
Daniel Paul

Reputation: 202

The top answer at the time I'm writing this which suggests using dashes works, but for anybody looking for something cleaner that both doesn't require setting up your user facing dates in a particular way or doing manual string editing I'd suggest using DateTime::createFromFormat

$dateObject = DateTime::createFromFormat('d/m/Y H:i:s', '07/09/2009 17:01:27');
echo $dateObject->format('Y-m-d');

I definitely recommend reading the docs for that function though, it has a lot of edge cases that might cause issues for your use case.

Upvotes: 0

John Conde
John Conde

Reputation: 219834

DateTime can handle this easily:

$datetime = new DateTime('07/09/2009 17:01:27', new DateTimeZone('Europe/London'));
echo $datetime->format('d/m/Y H:i:s');

See it in action

Upvotes: 4

l3ku
l3ku

Reputation: 211

Change / for -, seems like PHP gets / as American format, and - as European. You can see here:

$date = "06/10/2011 14:28"; // 6 october 2011 2:28 pm
$otherDate = "06-10-2011 14:28"; // 6 october 2011 2:28 pm

echo $stamp = strtotime($date) . "<br />"; // outputs 1307708880
echo $otherStamp = strtotime($otherDate) . "<br />"; // outputs 1317904080

echo date("d-m", $stamp); // outputs 10-06
echo date("d-m", $otherStamp); // outputs 06-10

http://www.php.net/manual/es/function.strtotime.php#106043

Upvotes: 21

John Carter
John Carter

Reputation: 55271

Providing you're not on Windows you can use strptime instead, and explicitly set the format. As OIS points out, strptime isn't available on Windows.

To convert from the split up year/month/day etc values to a unix timestamp you can pass them into mktime. Slightly fiddly, but it works.

One reason you might want to do this would be to print it in a different format using date.

From the strptime php.net page:

Example #1 strptime() example

<?php
  $format = '%d/%m/%Y %H:%M:%S';
  $strf = strftime($format);

  echo "$strf\n";

  print_r(strptime($strf, $format));
?>

The above example will output something similar to:

03/10/2004 15:54:19

Array
(
    [tm_sec] => 19
    [tm_min] => 54
    [tm_hour] => 15
    [tm_mday] => 3
    [tm_mon] => 9
    [tm_year] => 104
    [tm_wday] => 0
    [tm_yday] => 276
    [unparsed] =>
)

Upvotes: 6

OIS
OIS

Reputation: 10033

Here is a quick fix which should work. An alternative would be regex with named matches.

function eurototime($string)
{
    static $sorted = array(
        'tm_hour' => null,
        'tm_min' => null,
        'tm_sec' => null,
        'tm_mon' => null,
        'tm_mday' => null,
        'tm_year'  => null,
    );
    static $unsorted = array(
        'tm_mday',
        'tm_mon',
        'tm_year',
        'tm_hour',
        'tm_min',
        'tm_sec',

    );
    static $format = '%d/%d/%d %d:%d:%d';
    $parsed = sscanf($string, $format);
    $data = array_combine($unsorted, $parsed);
    $sortedData = array_merge($sorted, $data);
    $int = call_user_func_array('mktime', $sortedData);
    return $int;
}

date_default_timezone_set('Europe/London');
echo eurototime(date('d/m/Y H:i:s')), "\n", time(),"\n";

Upvotes: 1

robjmills
robjmills

Reputation: 18598

try:

strtotime(07/09/2009 17:01:27 GMT);

or try this first maybe:

date_default_timezone_set('Europe/London');

i guess the best answer is to not use strtotime as you can never be 100% with its accuracy

Upvotes: 1

Related Questions