PHP Date Function Seven days previous
Use the strtotime
method provided by PHP.
date('Y-m-d', strtotime('-7 days'))
Thanks to @lonesomeday for pointing out my mistake in the comments ;)
php - add + 7 days to date format mm dd, YYYY
$date = "Mar 03, 2011";
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);
Get Dates of the Last 7 Days in Array In Custom Format
function getLastNDays($days, $format = 'd/m'){
$m = date("m"); $de= date("d"); $y= date("Y");
$dateArray = array();
for($i=0; $i<=$days-1; $i++){
$dateArray[] = '"' . date($format, mktime(0,0,0,$m,($de-$i),$y)) . '"';
}
return array_reverse($dateArray);
}
Usage:$arr = getLastNDays(7);
or$arr = getLastNDays(7, 'd/m/Y');
Return current date plus 7 days
strtotime
will automatically use the current unix timestamp to base your string annotation off of.
Just do:
$date = strtotime("+7 day");
echo date('M d, Y', $date);
Added Info For Future Visitors: If you need to pass a timestamp to the function, the below will work.This will calculate 7 days
from yesterday:
$timestamp = time()-86400;
$date = strtotime("+7 day", $timestamp);
echo date('M d, Y', $date);
generating dates of previous seven days in php
Easiest way would be to get a timestamp that corresponds to today, using the time()
function ; and 7 times remove 1 day to that timestamp, each echoing the date that corresponds to that timestamp :
$timestamp = time();
for ($i = 0 ; $i < 7 ; $i++) {
echo date('Y-m-d', $timestamp) . '<br />';
$timestamp -= 24 * 3600;
}
Which will get you this output :2010-02-25
2010-02-24
2010-02-23
2010-02-22
2010-02-21
2010-02-20
2010-02-19
Because a timestamp only represents the number of seconds since 1970-01-01
, substracting one day to a timestamp means substracting 24*3600
seconds.Edit after the comments :
For month and weeks changes, this will still work : one day is still 24*3600 seconds.
For instance, if you test using this line instead of the first one I posted in my first example :
$timestamp = strtotime('2010-02-03');
You'll get this output :2010-02-03
2010-02-02
2010-02-01
2010-01-31
2010-01-30
2010-01-29
2010-01-28
It changed of month fine.
For leap years, if I test using this :
$timestamp = strtotime('2008-03-03');
I get :2008-03-03
2008-03-02
2008-03-01
2008-02-29
2008-02-28
2008-02-27
2008-02-26
Which looks fine, as there were 29 days in february 2008.And if I test with :
$timestamp = strtotime('2009-03-03');
I get :2009-03-03
2009-03-02
2009-03-01
2009-02-28
2009-02-27
2009-02-26
2009-02-25
Which looks fine too. DateTime 7 days from now at 00:00
Just use DateTime and DateInterval.
Example:
$date = new DateTime();
$date->sub(new \DateInterval('P7D'));
echo $date->format('Y-m-d 00:00:00');
Finding last 7 working days in PHP
Finding holidays will be more complicated.
One solution can be like saving the holidays in advance and skip them if they comes in the condition.
One simple solution for your problem can be like this.
<?php
$holiday = array(
'2017-12-16' => 'Victory Day of Bangladesh',
'2017-12-25' => 'Christmas'
);
$i = 0;
$work_day = '2017-12-26';
while($i != 7)
{
$work_day = date('Y-m-d', strtotime('-1 day', strtotime($work_day)));
$day_name = date('l', strtotime($work_day));
if($day_name != 'Saturday' && $day_name != 'Sunday' && !isset($holiday[$work_day]))
{
echo $work_day."\n";
$i++;
}
}
?>
Checking if a date is within 7 days of the current time
if( strtotime($rows['Date']) > strtotime('-7 day') ) {
echo '<div class="l-new"><a href="#"><!-- --></a></div>';
}
The strtotime('-7 day')
part returns the time 7 days ago.
Related Topics
How to Get the Root Url of the Site
Upload Progress Using Pure PHP/Ajax
How to Avoid Request Entity Too Large 413 Error
SQL - Insert and Catch the Id Auto-Increment Value
How Would You Transform a Pre-Existing Web App into a Multilingual One
How to Add Filter or Hook for "Woocommerce_Add_To_Cart"
Laravel Errno 150 Foreign Key Constraint Is Incorrectly Formed
Possible to View PHP Code of a Website
Doctrine2 Findby Relationship Object Triggers String Conversion Error
Replace Any Url's Within a String of Text, to Clickable Links with PHP
Make Woocommerce Checkout Phone Field Optional Based on Shipping Country
Str_Replace() for Multiple Value Replacement
How to Build a Condition Based Query in Laravel
What Is the Correct Format for a Blowfish Salt Using PHP's Crypt