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.

19-02-20:Wed
18-02-20:Tue
17-02-20:Mon
16-02-20:Sun
15-02-20:Sat
14-02-20:Fri
13-02-20:Thu
12-02-20:Wed
11-02-20:Tue
10-02-20:Mon
09-02-20:Sun
08-02-20:Sat
07-02-20:Fri
06-02-20:Thu
05-02-20:Wed
04-02-20:Tue

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
02-19-2020: Wed
02-20-2020: Thu
02-21-2020: Fri
02-22-2020: Sat
02-23-2020: Sun
02-24-2020: Mon
02-25-2020: Tue
02-26-2020: Wed
02-27-2020: Thu
02-28-2020: Fri
02-29-2020: Sat
03-01-2020: Sun
03-02-2020: Mon
03-03-2020: Tue
03-04-2020: Wed
03-05-2020: Thu


plus2net.com






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 your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer