Publishing System Settings Logout Login Register
How get yesterday date,last hour on PHP?
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on April 16th, 2010
4000 views
PHP Coding

Step 1

We need set format data in function date():
Function date() returns a string formatted according to the givenformat string using the given integer timestamp or the current time ifno timestamp is given. In other words, timestampis optional anddefaults to the value of time().

<?
echo date("F j, Y");
?>

result:

March 30, 2010

Step 2

For "yesterday" date use php function mktime():
Function mktime() returns the Unix timestamp corresponding to thearguments given. This timestamp is a long integer containing the numberof seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and thetime specified.
Arguments may be left out in order from right to left; any argumentsthus omitted will be set to the current value according to the localdate and time.

<?
echo mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
?> 

result:

1269820800

Step 3

Now merge all and look at this:

<? 
$yesterday = date("Y-m-d", mktime(0, 0, 0, date("m") , date("d")-1,date("Y")));
echo $yesterday;
?> 

result:

March 29, 2010 



Operating similarly, it is possible to receive time hour back.

<?
$yesterday = date("H:i:s",mktime(date("H"), 0, 0, date("m"),date("d"), date("Y")));
echo $yesterday;
?> 

result:

20:00:00 



or 7 days ago:

<?
$week = date("Y-m-d",mktime(0, 0, 0, date("m"), date("d")-7,date("Y")));
echo $week;
?> 

result:

2010-03-23 
It's all. Thank you!
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
nohlp

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top