Friday, July 12, 2013

How to display the previous days date using PHP

Hi to every one. Today am going to give a small code to show the previous day date using the PHP. Just copy and paste the below code into your editor and change the static number to your number.
date('Y/m/d', strtotime('-N days',strtotime(date('Y/m/d'))));

date('Y/m/d') will gives current date.
strtotime(date('Y/m/d')) will convert the current date to UNIX Timestamp.
Here '-' represents the previous days to show. 
Here 'N' is the number to show 'Nth' days ago date from the current date.
'-N days' will gives the previous 'Nth' day date.
strtotime('-N days',strtotime(date('Y/m/d')) will gives the UNIX Timestamp string of previous 'Nth' day date.
date('Y/m/d', strtotime('-N days',strtotime(date('Y/m/d')))) will display the UNIX Timestamp to date format of YYYY/MM/DD.

For example:
Assume that current date is 2013/07/12. and if we want to display 7 days ago date then write the following code.
date('Y/m/d', strtotime('-7 days',strtotime(date('Y/m/d'))));
It will show the last 7 days ago date in the format of 2013/07/05.


No comments:

Post a Comment