Step 1
We need format format data in the date () function: The date () function returns a string formatted according to the given format string using the given timestamp or the current time ifno timestamp. In other words, timestampis is optional and points to the value of time ().
<?php echo date("F j, Y"); ?>
Result: March 30, 2010
Step 2
For the "yesterday" date, use the php function mktime (): The mktime () function returns the Unix timestamp corresponding to the given parameters. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1, 1970 00:00:00 GMT) and the specified time. Arguments can be omitted from right to left; any argument omitted will be set to the current value according to localdate and time.
<?php echo mktime(0, 0, 0, date("m"), date("d")-1, date("Y")); ?>
Result: 1269820800
Step 3
Now combine everything and look at this:
<?php $yesterday = date("Ymd", mktime(0, 0, 0, date("m") , date("d")-1,date("Y"))); echo $yesterday; ?>
Result: March 29, 2010
Acting in the same way, you can get the time back.
<?php $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:
<?php $week = date("Ymd",mktime(0, 0, 0, date("m"), date("d")-7,date("Y"))); echo $week; ?>
Result: 2010-03-23