oh how frustrating ... date()

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
HormonX
Forum Commoner
Posts: 50
Joined: Tue Dec 10, 2002 7:43 pm
Location: Toronto

oh how frustrating ... date()

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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";
Simon
Forum Newbie
Posts: 12
Joined: Sun Dec 12, 2004 11:36 pm

Post 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 >
Post Reply