Getting yesterday and previous days date values

Calculate previous dates with DateTimeImmutable

For calendar-day calculations, using DateTimeImmutable and relative modification is clearer than subtracting a fixed number of seconds.

$today = new DateTimeImmutable('today');
for ($i = 1; $i <= 7; $i++) {
    echo $today->modify("-$i day")->format('Y-m-d D') . "<br>";
}
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.

11-10-26:Sun
10-10-26:Sat
09-10-26:Fri
08-10-26:Thu
07-10-26:Wed
06-10-26:Tue
05-10-26:Mon
04-10-26:Sun
03-10-26:Sat
02-10-26:Fri
01-10-26:Thu
30-09-26:Wed
29-09-26:Tue
28-09-26:Mon
27-09-26:Sun
26-09-26:Sat

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
10-11-2026: Sun
10-12-2026: Mon
10-13-2026: Tue
10-14-2026: Wed
10-15-2026: Thu
10-16-2026: Fri
10-17-2026: Sat
10-18-2026: Sun
10-19-2026: Mon
10-20-2026: Tue
10-21-2026: Wed
10-22-2026: Thu
10-23-2026: Fri
10-24-2026: Sat
10-25-2026: Sun
10-26-2026: Mon
PHP Date Functions Adding Date by using DateInterval Displaying dropdown list to select Month Year & Date


Subscribe to our YouTube Channel here



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




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