Getting yesterday and previous days date values

You must have seen how to get today date by using date function. By changing the date value we can get the dates of yesterday. Same way we can extend this to get date values of last 7 days. Or we can extend this to get dates between any to differences.

Let us find out the date value of yesterday.

$m= date("m"); // Month value
$de= date("d"); //today's date
$y= date("Y"); // Year value
echo date('d-m-y:D', mktime(0,0,0,$m,($de-1),$y));
Last seven days date value we can find out by using a for loop. Here is the output of the code.

29-08-16:Mon
28-08-16:Sun
27-08-16:Sat
26-08-16:Fri
25-08-16:Thu
24-08-16:Wed
23-08-16:Tue
22-08-16:Mon
21-08-16:Sun
20-08-16:Sat
19-08-16:Fri
18-08-16:Thu
17-08-16:Wed
16-08-16:Tue
15-08-16:Mon
14-08-16:Sun

Here is the code.

$m= date("m");
$de= date("d");
$y= date("Y");
for($i=0; $i<=15; $i++){
echo date('d-m-y:D',mktime(0,0,0,$m,($de-$i),$y));
echo "<br>";
}

Using strtotime()

We can create unixtime stamp from text string by using strtotime()

for($i=0; $i<=15; $i++){

echo date('m-d-Y: D',strtotime("$i day"))."<br>";

}
Output is here
08-29-2016: Mon
08-30-2016: Tue
08-31-2016: Wed
09-01-2016: Thu
09-02-2016: Fri
09-03-2016: Sat
09-04-2016: Sun
09-05-2016: Mon
09-06-2016: Tue
09-07-2016: Wed
09-08-2016: Thu
09-09-2016: Fri
09-10-2016: Sat
09-11-2016: Sun
09-12-2016: Mon
09-13-2016: Tue

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Your Rating


Google+


More On Date Object
bhanu

30-09-2009

thank you for help.it very help
siswoutomo

28-12-2009

Very good this post, thank lot of
James

21-04-2010

Does this work if the date is 4th of January (for example)
smo

02-05-2010

Yes this will work perfectly. Try with March 1st of 2008 also. For testing you need to change the date of your PC or server.
kalpana

15-01-2013

thanks you so much.It was so helpful

Post Comment This is for short comments only. Use the forum for more discussions.




Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2016 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer