Page 1 of 1

oh how frustrating ... date()

Posted: Mon Dec 13, 2004 9:18 pm
by HormonX
am getting to the point to tossing my laptop out the window. This seems to be simple and it works and it doesn't. All am trying to do is to get current date and put that in to the database , also i want to take the same date and add 14 days to it. here is what i do ...

to get current date i use

Code: Select all

time();
and to get todays date plus 14 day i use

Code: Select all

mktime(0,date("i"),date("H"),date("m"),date("d")+14,date("Y"))
.

When i print these to the screen i get correct dates and it works just fine. now when i put these dates in the db and try to explode them to the format i need i get 31 12 69.

To explode the date i use

Code: Select all

date('d m y' , $var)
$var is date from the database.

Please help ...

Greg

Posted: Mon Dec 13, 2004 9:47 pm
by timvw
this is how i would do it:

Code: Select all

$current = strtotime("now");
$later = strototime("now +14 DAYS");
if the database column is datetime or timestamp i use mysql from_unixtime function:

Code: Select all

$query = "INSERT INTO table VALUES (FROM_UNIXTIME($current))";
if i want to get them back out i use mysql date_format (or if i wanted to use it in php again: unix_timestamp)

Code: Select all

$query = "SELECT DATE_FORMAT(date, '%Y %m %d') AS date FROM table";

Posted: Mon Dec 13, 2004 10:04 pm
by Simon
Yeah, I'd do it similar to Tim, though I usually do date calculations manually, like...

Code: Select all

$current = time();      // number of seconds since 1 Jan 1970
$later = time() + 14*24*60*60;   // 14 days * 24 hours * 60 mins * 60 seconds
Tim's way's probably easier, I had forgotten about the ability to do that.

< Simon >