Jump to content

Increase Date Value


superdan_35

Recommended Posts

Hi all.

 

I currently have a database that stores various details relating to each day in a business. I want to be able to take the last date something was entered and display a form for the user to enter the details for the next seven days.

 

I have selected the most recent value from the database, which is of type date("Y-m-d"), but now I cannot work out how to increase it.

 

I have tried to add 1 but that increases it by 1 year, I have tried to increase it by seconds but as it's not a timestamp that doesn't work. I have searched around for a while and had no luck.

 

So I have considered splitting up the date, increasing the day value and putting it back together, but before I try that does anyone have any suggestions?

 

Thanks very much,

Dan

Link to comment
https://forums.phpfreaks.com/topic/154885-increase-date-value/
Share on other sites

$unixTime = strtotime($date);

 

Gives you the time in seconds since the unix epoch.

 

As it's in seconds you can add 60 to it to add a minute, add (60 * 60) to add an hour etc etc.

 

It's common when you're doing this to split it down like that so it's easy to read for the next person looking at your code.

 

So to add a day:

 

$unixTime = $unixTime + (60 * 60 * 24);

 

There are other ways but this is easy and reliable. To get it back to Y-m-d just do:

 

echo date('Y-m-d', $unixTime);

Link to comment
https://forums.phpfreaks.com/topic/154885-increase-date-value/#findComment-814654
Share on other sites

You can use a for loop with date and strtotime

 

<?php
$start_date = '2009-04-30';
$st = strtotime($start_date);
$end = strtotime('+7 days',$st);
for ($i=$st;$i<$end;$i += 86400) { // 86400 seconds in a day
    echo date('l, F jS, Y',$i) . '<br>';
}
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/154885-increase-date-value/#findComment-814660
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.