Pergunta

I am currently trying to get a database transfered from an old system to a new one, and i have run into a small issue.

I have a db row formatted at:

2007-04-24 00:23:59 (yyyy-mm-dd hh:mm:ss)

And i cant seem to get it to work through strptime()

Here is what i have:

$format = '%Y-%m-%d %H:%i:%s';
$ct = strptime($row['time'], $format );
//$row['time'] == 2007-01-11 00:47:27

This returns nothing.

Can anyone see anything wrong with that?

Thanks.

Foi útil?

Solução

Format should be a bit different:

$format = '%Y-%m-%d %H:%M:%S'; // or just '%F %T'

Here's the corresponding section of strftime format documentation:

%M  Two digit representation of the minute  [00 through 59]
%S  Two digit representation of the second  [00 through 59]
%T  Same as "%H:%M:%S"
%F  Same as "%Y-%m-%d" (commonly used in database datestamps)

Outras dicas

Refer to the strftime() documentation for the proper format.

Your foramt should be:

$format = '%Y-%m-%d %T';

From manual http://www.php.net/manual/en/function.strptime.php

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

echo "$strf\n";

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

Result

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] =>
)

strptime() is not available in Windows.

Note: This function is not implemented on Windows platforms.

Source: http://php.net/manual/en/function.strptime.php

This is how I usually handle date formatting.

$somedate = strtotime($dateValue);
$formatted_date = date('Y-m-d H:i:s', $somedate);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top