Getting dates

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
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Getting dates

Post by mhouldridge »

Hi,

To get the date in php I use....

Code: Select all

date("Y-m-d H:i:s");
I would like to retreive a date value 30days in the past. How could I do this?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

strtotime() will be able to help you out. many other people will post the code themselves after me but i think its a good idea to learn how that function works and how you can use it with date() to get different dates as this is somthing that people have to deal with very very often.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

echo date("Y-m-d H:i:s", strtotime("-1 day"));
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Thanks for this.

Kinda got it working now, and know where to go from here.

Just wondering also. I looked on the Internet about timestamps. Here is some code I saw;

Code: Select all

// timestamp for 13:56:04 on 18 Jun 2003 
echo mktime(13,56,04,06,18,2003)."<br>"; 
echo date("M d Y, H:i:s",mktime(13,56,04,06,18,2003))."<br>"; 

// timestamp for 13:56:04 on 18 Jun 2003 + 18 days 
echo mktime(13,56,04,06,36,2003)."<br>"; 
echo date("M d Y, H:i:s",mktime(13,56,04,06,36,2003))."<br>";
Why do people use timestamps method instead of....

Code: Select all

echo date("Y-m-d H:i:s", strtotime("-1 month"));

which seems far cleaner?

I dont quite grasp the difference between timestamp use and strtotime?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I guess strtotime cannot parse complex date input formats.
Post Reply