PHP to get yesterday(previous) date

PHP to get yesterday(previous) date:

Here are some methods to get the previous date using php. i.e.,)
yesterday date.
Here date() function is used and “D d-M-Y” format is used, it will
return the date format like “MON 29-Jul-2013”.

Let we go to some examples to get the yesterday date in php.

1. echo date(“D d-M-Y”, time() – 60 * 60 * 24);

2. $pDate = new DateTime();
$pDate->add(DateInterval::createFromDateString(‘yesterday’));
echo $pDate->format(‘D d-M-Y’);

3. echo date(“D d-M-Y”, strtotime(“yesterday”));

4. echo date(“D d-M-Y”, strtotime(‘-1 days’));

5. echo date(‘D d-M-Y’, time() – 86400);

6. echo date(“D d-M-Y”, mktime(0, 0, 0, date(“m”) , date(“d”)-1,date(“Y”)));

These all are return the date in same format like “MON 29-Jul-2013”;

If you want some other date format then consider the below examples.

date(“D d-M-Y”); // TUE 30-Jul-2013
date(“F j, Y, g:i a”); // July 30, 2013, 3:10 pm
date(“m.d.y”); // 07.30.13
date(“j, n, Y”); // 30, 7, 2013
date(“Y-m-d H:i:s”); // 2013-07-30 11:22:14
date(‘h-i-s, j-m-y, it is w Day’); // 03-10-41, 30-07-13, 2031
2045 2 Tueam13
date(‘it is the jS day.’); // it is the 30th day.
date(“D M j G:i:s T Y”); // Tue Jul 30 11:21:03 CEST 2013
date(‘H:m:s m is month’); // 11:07:36 m is month
date(“H:i:s”); // 11:21:48
date(“Ymd”); // 20130730

Here you can see the attribute are used in date() function is “D,d,m,y,Y,M,H,m,s,i,a,A”.
so you can modify it as what format you want like “date(D d/m/Y)”. etc…

If you have any queries comments it. Sorry for my poor english.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *