d.m.y -> unix time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

d.m.y -> unix time

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: d.m.y -> unix time

Post 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]));
?>
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: d.m.y -> unix time

Post by JKM »

Yeah, I ment $date[2]. Still not working.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: d.m.y -> unix time

Post 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]);
Post Reply