views:

921

answers:

5

Hi,

Is there a very simple way in PHP to convert one date format into another date format?

I have this:

$old_date = date('y-m-d-h-i-s');              // works

$middle = strtotime($old_date);              // returns (bool)false

$new_date = date('Y-m-d H:i:s', $middle);   // returns 1970-01-01 00:00:00

But I'd of course like it to return a current date rather than the crack 'o dawn. What am I doing wrong?

Thank you.

+5  A: 

The second parameter to date() needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can't recognize.

You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn't recognize the y-m-d-h-i-s format.

PHP 5.3 and up

Use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.

PHP 5.2 and lower

You will have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp.

But that's a lot of work! I recommend using a different format that strftime() can understand. strftime() understands any date input short of the next time joe will slip on the ice. for example, this works:

$old_date = date('l, F d y h:i:s');              // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);   
Pekka
Thanks Pekka, just edited my question, tried that but it doesnt seem to work.
Tom
I edited the answer while you accepted it :) I added some more examples and references.
Pekka
Gotcha, that's indeed the problem. It's a fixed filename I need to convert back into a date (has to be in that format), so I'll figure out a workaround.
Tom
+1  A: 

You need to convert the $old_date back into a timestamp, as the date function requires a timestamp as its second argument.

middaparka
+2  A: 

Try this:

$old_date = date('y-m-d-h-i-s');
$new_date = date('Y-m-d H:i:s', strtotime($old_date));
Sarfraz
Won't work, strtotime() doesn't recognize the format. Just tried.
Pekka
agreed, i just put in the format code from questioner, there should be proper format specified.
Sarfraz
+1  A: 

Everything seems to be working fine, check it @ IDEOne.

Alix Axel
Ok, will check my code, thanks.
Tom
Odd, doesn't work for me locally (PHP 5.3.0 on Windows 7). `strtotime` returns 0.
Pekka
@Pekka: I haven't tried it locally but it's possible, the `strtotime()` has dependencies with the OS.
Alix Axel
Didnt actually work for me locally either: PHP 5.3.1 on Win XP ... hehe.
Tom
+1  A: 
$old_date = date('y-m-d-h-i-s');       // works

you are doing wrong here, this should be

$old_date = date('y-m-d h:i:s');       // works

separator of time is ':'


I think this will help...

$old_date = date('y-m-d-h-i-s');              // works

preg_match_all('/(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)/', $old_date, $out, PREG_SET_ORDER);
$out = $out[0];
$time = mktime($out[4], $out[5], $out[6], $out[2], $out[3], $out[1]);

$new_date = date('Y-m-d H:i:s', $time); 

OR


$old_date = date('y-m-d-h-i-s');              // works

$out = explode('-', $old_date);
$time = mktime($out[3], $out[4], $out[5], $out[1], $out[2], $out[0]);

$new_date = date('Y-m-d H:i:s', $time); 
Rifat
Yes, sure thanks, it's a GUID filename that I want to convert back into a proper date format.
Tom
Thanks, have something similar now.
Tom