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.

06-12-22:Tue
05-12-22:Mon
04-12-22:Sun
03-12-22:Sat
02-12-22:Fri
01-12-22:Thu
30-11-22:Wed
29-11-22:Tue
28-11-22:Mon
27-11-22:Sun
26-11-22:Sat
25-11-22:Fri
24-11-22:Thu
23-11-22:Wed
22-11-22:Tue
21-11-22:Mon

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
12-06-2022: Tue
12-07-2022: Wed
12-08-2022: Thu
12-09-2022: Fri
12-10-2022: Sat
12-11-2022: Sun
12-12-2022: Mon
12-13-2022: Tue
12-14-2022: Wed
12-15-2022: Thu
12-16-2022: Fri
12-17-2022: Sat
12-18-2022: Sun
12-19-2022: Mon
12-20-2022: Tue
12-21-2022: Wed

Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    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-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer