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.

27-04-17:Thu
26-04-17:Wed
25-04-17:Tue
24-04-17:Mon
23-04-17:Sun
22-04-17:Sat
21-04-17:Fri
20-04-17:Thu
19-04-17:Wed
18-04-17:Tue
17-04-17:Mon
16-04-17:Sun
15-04-17:Sat
14-04-17:Fri
13-04-17:Thu
12-04-17:Wed

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
04-27-2017: Thu
04-28-2017: Fri
04-29-2017: Sat
04-30-2017: Sun
05-01-2017: Mon
05-02-2017: Tue
05-03-2017: Wed
05-04-2017: Thu
05-05-2017: Fri
05-06-2017: Sat
05-07-2017: Sun
05-08-2017: Mon
05-09-2017: Tue
05-10-2017: Wed
05-11-2017: Thu
05-12-2017: Fri

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
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-2017 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer