Jump to content


Photo

Most annoying problem with dates


  • Please log in to reply
4 replies to this topic

#1 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 18 June 2006 - 12:26 PM

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)

$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>";

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?

http://thewebsolutionprovider.com - A Solution For Everything Web!

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 18 June 2006 - 02:18 PM

Try:
$date = date("d-m-Y", mktime(0,0,0,$exploded[1],$exploded[2],$exploded[0]));

Legend has it that reading the manual never killed anyone.
My site

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 18 June 2006 - 02:22 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]mktime

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
~ D Kuang

#4 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 18 June 2006 - 03:50 PM

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
http://thewebsolutionprovider.com - A Solution For Everything Web!

#5 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 18 June 2006 - 03:53 PM

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]
~ D Kuang




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users