Simplest way to increment a date in PHP?
A clean way is to use strtotime()
$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);
Will give you the 2007-03-01
How can I increment a date by one day in Php?
You can use strtotime.
$your_date = strtotime("1 day", strtotime("2016-08-24"));
$new_date = date("Y-m-d", $your_date);
Hope it will help you.
PHP Incrementing date with day name
You don't need to parse the week day name to add days onto a date.
$date = "Saturday/02-05-2015";
list($weekdayName, $dateString) = explode("/", $date, 2); // Parse "02-05-2015"
$dateObj = new \DateTime($dateString);
$dateObj->add(new \DateInterval("P1D")); // P1D stands for "Period 1 Day"
echo $dateObj->format("l/d/m/Y"); // Sunday/03/05/2015
I used the DateTime
class, here is the documentation.
I wrote out what you are trying to do yourself, not sure what is causing your issue. This code works though.
$date = "Friday/01-05-2015";
list($weekdayName, $dateString) = explode("/", $date, 2); // Parse "01-05-2015"
$dateObj = new \DateTime($dateString);
for($i = 0; $i < 5; $i++) {
$dateObj->add(new \DateInterval("P1D")); // P1D stands for "Period 1 Day"
echo $dateObj->format("l/d/m/Y") . '<br>';
}
Outputs:
Saturday/02/05/2015
Sunday/03/05/2015
Monday/04/05/2015
Tuesday/05/05/2015
Wednesday/06/05/2015
PHP - Increment Dates in Loop
You can try below code
$begin = new DateTime('2013-02-01');
$end = new DateTime('2013-02-13');
$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
foreach($daterange as $date){
echo $date->format("d") . "<br>";
}
increment date by one month
$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));
// Finally you will have the date you're looking for.
use php to increment a date one week at a time with 10 iterations
May be you are looking for this: Online Example
$start_date = "06/25/2012";
$date = strtotime($start_date);
$X=1;
while ($X <= 10) {
$X++;
$date = strtotime("+1 weeks", $date);
echo date('m/d/Y', $date)."<br>";
}
How to dynamically increment dates using strtotime?
How about this:
// initialize an array with your first date
$dates = array(strtotime("+17 days", strtotime("2017-04-03")));
// now loop 26 times to get the next 26 dates
for ($i = 1; $i <= 26; $i++) {
// add 14 days to previous date in the array
$dates[] = strtotime("+14 days", $dates[$i-1]);
}
// echo the results
foreach ($dates as $date) {
echo date("Y-m-d", $date) . PHP_EOL;
}
Incrementing date in a loop with php
It's safe to use date('d') + 1
in function mktime
, but it's better to use strtotime
instead..
$time = time();
echo date( 'd/m/Y', $time);
$time = strtotime('+1 day', $time);
echo date( 'd/m/Y', $time);
update
$time = time();
for ($i = 0; $i < 3; $i++) {
echo date( 'd/m/Y', $time);
$time = strtotime('+1 day', $time);
}
increment the date
$i = 1;
while (your condition here)
{
echo date('Y-m-d', strtotime('+' . $i++ . ' day'));
}
Related Topics
Codeigniter Cron Job Not Working
Increase Max Execution Time for PHP
Woocommerce: Add Product to Cart with Price Override
Using PHP and Google Maps API to Work Out Distance Between 2 Post Codes (Uk)
In PHP, Is There a Short Way to Compare a Variable to Multiple Values
Uploading File in PHP Server from Android Device
PHP Commands Out of Sync Error
Woocommerce - Hide Other Shipping Methods When Free Shipping Is Available
How to Call a Closure That Is a Class Variable
How to Dynamically Write a PHP Object Property Name
Convert All Types of Smart Quotes with PHP
What Is Your Preferred PHP Deployment Strategy
How to Render a Datetime Object in a Twig Template