Jump to content

Archived

This topic is now archived and is closed to further replies.

glenelkins

Most annoying problem with dates

Recommended Posts

ok so i have written a little test script as im having problems with inserting dates into a date field in the MySql db. It always seems to put any date into the db as: 0000-00-00 or something wierd like 2017-12-05 or 1970-12-05 etc

here is the small test code (line by line to see what is happening)

[code]
$inputdate = "2006-06-18";
echo "INPUT DATE: " . $inputdate . "<br>";
    
$exploded = explode("-",$inputdate);
    
echo "DAY: " . $exploded[2] . " MONTH: " . $exploded[1] . " YEAR: " . $exploded[0] . "<br>";
    
    
$date = date("d-m-Y", mktime(0,0,0,0,$exploded[1],$exploded[2],$exploded[0]));
    
echo "DATABASE DATE: " . $date . "<BR>";
[/code]

I get this output:

INPUT DATE: 2006-06-18
DAY: 18 MONTH: 06 YEAR: 2006
DATABASE DATE: 05-12-2017

This is so annoying after writing a big website and this one little thing is not working. Any ideas people?

Share this post


Link to post
Share on other sites
Try:
[code]$date = date("d-m-Y", mktime(0,0,0,$exploded[1],$exploded[2],$exploded[0]));[/code]

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--][b]mktime[/b]

Get Unix timestamp for a date (PHP 3, PHP 4, PHP 5)

int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )[/quote]

[a href=\"http://www.php.net/mktime\" target=\"_blank\"]http://www.php.net/mktime[/a]

You've got an extra zero

Share this post


Link to post
Share on other sites
Well thats now working fine, but its still entering into the database as 0000-00-00 does this have anything to do with changing the format of the date? im trying to enter a uk date rather than us

Share this post


Link to post
Share on other sites
Maybe you are using a MySQL timestamp data type?
In this case you shouldn't insert a UNIX timestamp...

[a href=\"http://dev.mysql.com/doc/refman/5.0/en/datetime.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/datetime.html[/a]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.