Monday 2 February 2015

Getting yesterday and previous days date values - PHP

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. 

02-02-15:Mon
01-02-15:Sun
31-01-15:Sat
30-01-15:Fri
29-01-15:Thu
28-01-15:Wed
27-01-15:Tue
26-01-15:Mon
25-01-15:Sun
24-01-15:Sat
23-01-15:Fri
22-01-15:Thu
21-01-15:Wed
20-01-15:Tue
19-01-15:Mon
18-01-15: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>";
}

0 comments:

Post a Comment