cvzyl Posted March 7, 2007 Share Posted March 7, 2007 Hi I am a newbie to PHP so if this is a really dumb question, I apologise in advance. I have a value stored in variable $date and want to increment this date by one day, something like: $date = $date + 1; I cannot find a date increment function in the PHP help file. I thought that this would be quite a commonly used function but maybe I am looking in the wrong place. How should I go about doing this? Thanks in advance Cobus Quote Link to comment https://forums.phpfreaks.com/topic/41677-date-increment/ Share on other sites More sharing options...
Ninjakreborn Posted March 7, 2007 Share Posted March 7, 2007 Turn it into a timestamp (run it through the strtotime function), and then add "UTC days are mostly 86400 s long, but are occasionally 86401 s and could be 86399 s long" - referenced from <a href="http://en.wikipedia.org/wiki/Unix_time">HERE</a> to it Quote Link to comment https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-201953 Share on other sites More sharing options...
JBS103 Posted March 7, 2007 Share Posted March 7, 2007 $date = strtotime("+1 day"); Will give tommorow based off the current time. $date += 86400; Will give any $date plus 1 day assuming you are working off seconds. Quote Link to comment https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-201956 Share on other sites More sharing options...
Barand Posted March 7, 2007 Share Posted March 7, 2007 I have a value stored in variable $date How you add 1 day depends on how that date variable is stored If it's in the form of a date, eg $date = "2007-03-07" ; $plus_one_day = date ('Y-m-d', strtotime ("+1 days $date")); If it's a unix timestamp in seconds you can add 86400 as above or $plus_one_day = date ('Y-m-d', strtotime ("+1 days", $date)); The advantage of the strtotime method is the increment can be "+x days" "+x hours" "-x days" etc etc withour you having to calculate the number of seconds. Quote Link to comment https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-201961 Share on other sites More sharing options...
cvzyl Posted March 7, 2007 Author Share Posted March 7, 2007 Thanks for all the comments, I managed to get all the different methods to work but it seems like the suggestion from Barand $plus_one_day = date ('Y-m-d', strtotime ("+1 days", $date)); is the cleanest solution. My next problem is, how do I "terminate" a date to only contain Y-m-d without any hours or seconds. I want 2007-03-07 00:00:00 rather than 2007-03-07 23:25:37. I want to convert a variable (e.g. $date) to this "terminated" value. Cobus Quote Link to comment https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-201976 Share on other sites More sharing options...
Barand Posted March 7, 2007 Share Posted March 7, 2007 If it's a database field specify as DATE and not DATETIME. BTW, how are you storing that date variable? Quote Link to comment https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-201983 Share on other sites More sharing options...
cvzyl Posted March 7, 2007 Author Share Posted March 7, 2007 Barand The database field is defined as DATE and not DATETIME. What do you mean how am I storing the variable? Cobus Quote Link to comment https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-201998 Share on other sites More sharing options...
artacus Posted March 7, 2007 Share Posted March 7, 2007 Just do it when you are pulling it from the database SELECT DATE_ADD(myDate, INTERVAL 1 DAY) AS newDay Quote Link to comment https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-202003 Share on other sites More sharing options...
Barand Posted March 8, 2007 Share Posted March 8, 2007 The database field is defined as DATE and not DATETIME. In that case the time element will be 00:00:00 What do you mean how am I storing the variable? I meant as a date or unix time Quote Link to comment https://forums.phpfreaks.com/topic/41677-date-increment/#findComment-202124 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.