Page 1 of 1

d.m.y -> unix time

Posted: Tue Oct 06, 2009 6:54 pm
by JKM
I've got a lot of dates in d.m.y format. The time isn't that important. It could be d.m.y 00:00:01 or something. I tried this:

Code: Select all

<?php
    $date = explode('.', $fetch['date']);
    echo date('U', mktime(0, 0, 1, $date[1], $date[0], $date[3]));
?>
But it didn't work out that well. I also tried the example at php.net:

Code: Select all

<?php
echo date("M-d-Y", mktime(0, 0, 0, 12, 32, 1997));
echo date("M-d-Y", mktime(0, 0, 0, 13, 1, 1997));
echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 1998));
echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 98));
?>
This returns:
Jan-01-1998
Jan-01-1998
Jan-01-1998
Jan-01-1998

Re: d.m.y -> unix time

Posted: Tue Oct 06, 2009 6:59 pm
by Mark Baker
If your date is d.m.y
$date[3] will be null because there are only elements 0,1 and 2 in your exploded array

Code: Select all

<?php
    $date = explode('.', $fetch['date']);
    echo date('U', mktime(0, 0, 1, $date[1], $date[0], $date[2]));
?>

Re: d.m.y -> unix time

Posted: Tue Oct 06, 2009 7:45 pm
by JKM
Yeah, I ment $date[2]. Still not working.

Re: d.m.y -> unix time

Posted: Wed Oct 07, 2009 3:10 am
by Mark Baker
So what are you expecting to see?

Code: Select all

 
$fetch['date'] = '19.12.1960';
 
$date = explode('.', $fetch['date']);
echo date('U', mktime(0, 0, 1, $date[1], $date[0], $date[2]));
 
Gives -285119999, which is the correct value.
The only thing I can think of is if your month was a month name rather than a numeric value

Code: Select all

 
$fetch['date'] = '19.Dec.1960';
 
$date = explode('.', $fetch['date']);
echo date('U', mktime(0, 0, 1, $date[1], $date[0], $date[2]));
 
gives a warning: mktime() expects parameter 4 to be long, string given and an echoed result of 0


Not totally sure why you're formatting as 'U' though, because this should give the same value as

Code: Select all

echo mktime(0, 0, 1, $date[1], $date[0], $date[2]);