jeff5656 Posted February 1, 2012 Share Posted February 1, 2012 When I try to add 30 days: $date = date("Y-m-d"); $date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days"); echo $date; and I echo date I get 1330664400 How do I get it to echo out 3/1/2012? I know the answer lies in the strtotime but I can't figure it out. I know it's a simple problem for most of you... Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/ Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 strtotime() returns a unix timestamp, which is what the integer that you are receiving is, to convert it into the proper format, use date Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313320 Share on other sites More sharing options...
PFMaBiSmAd Posted February 1, 2012 Share Posted February 1, 2012 You have far to many function calls. To get the current date + 30 days - $date = date("Y-m-d", strtotime(" +30 days")); Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313323 Share on other sites More sharing options...
jeff5656 Posted February 1, 2012 Author Share Posted February 1, 2012 Thanks PFMaBiSmAd. That's so much simpler LOL Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313326 Share on other sites More sharing options...
ManiacDan Posted February 1, 2012 Share Posted February 1, 2012 Also note that 30 days from today is 3/2/12. A day is not always 145440 86400 seconds, a month is not always 30 days, a year is not always 365 days, etc. Use the dateTime::dateAdd() function if you have PHP 5.3 or above. Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313328 Share on other sites More sharing options...
scootstah Posted February 1, 2012 Share Posted February 1, 2012 A day is not always 145440 seconds A day is never 145440 seconds, it is 86400 seconds. Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313334 Share on other sites More sharing options...
ManiacDan Posted February 1, 2012 Share Posted February 1, 2012 A day is not always 145440 seconds A day is never 145440 seconds, it is 86400 seconds. That's what I get for posting before I have my coffee. Where did that number come from? Seriously. Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313341 Share on other sites More sharing options...
Maq Posted February 1, 2012 Share Posted February 1, 2012 A day is not always 145440 seconds A day is never 145440 seconds, it is 86400 seconds. That's what I get for posting before I have my coffee. Where did that number come from? Seriously. 1.68 days Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313344 Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 A day is not always 145440 seconds A day is never 145440 seconds, it is 86400 seconds. That's what I get for posting before I have my coffee. Where did that number come from? Seriously. 1.68 days then went on to say that 86400 seconds is not always a day, lol Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313351 Share on other sites More sharing options...
ManiacDan Posted February 1, 2012 Share Posted February 1, 2012 then went on to say that 86400 seconds is not always a day, lol It's not. That was my point. Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313373 Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 then went on to say that 86400 seconds is not always a day, lol It's not. That was my point. leap second..really Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313374 Share on other sites More sharing options...
scootstah Posted February 1, 2012 Share Posted February 1, 2012 then went on to say that 86400 seconds is not always a day, lol It's not. That was my point. Does strtotime() not account for this? Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313394 Share on other sites More sharing options...
ManiacDan Posted February 1, 2012 Share Posted February 1, 2012 AK: Yes, really. Time is incredibly complex in programming, and if you don't realize that there are things like leap seconds, leap days, and daylight savings you'll end up with "bugs" that you can't explain. Scoot: No. According to wikipedia and the screenshot of time.gov, there was a leap second at 2008-12-31 18:59:60 in EST. Adding 1 seconds to 2008-12-31 18:59:59 should give you 2008-12-31 19:00:00 but it does not: php > echo date('c', strtotime('2008-12-31 18:59:59 + 2 seconds')); 2008-12-31T19:00:01-05:00 PHP also doesn't correctly account for the leap second in the difference between the times: php > echo strtotime('2009-01-01 00:00:00') - strtotime('2008-12-31 00:00:00'); 86400 Although it's moot since the DateTime library doesn't respect the leap second either: php > $a = new DateTime('2009-01-01 00:00:00 EST'); php > $b = new DateTime('2008-12-31 00:00:00 EST'); php > $c = $a->diff($b); php > echo $c->format('%d days, %h hours, %m minutes, %s seconds'); 1 days, 0 hours, 0 minutes, 0 seconds What you really have to know is that computer scientists didn't have anything to do with writing the calendar. The exact size of a day, month, and year change (and the number of days in a week have been altered legislatively, but not in the last 300 years). You can be relatively certain that assuming there are 86400 seconds in a day, but you need to know it's not exactly right. The reason I posted in the first place is because the OP thought 30 days from a date in February was the same day of the month in March. It's not. There are 4 values for "length of a month in days" and unfortunately two values for "length of a day," even though the leap second isn't really all that important. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313404 Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 AK: Yes, really. Time is incredibly complex in programming, and if you don't realize that there are things like leap seconds, leap days, and daylight savings you'll end up with "bugs" that you can't explain. Scoot: No. According to wikipedia and the screenshot of time.gov, there was a leap second at 2008-12-31 18:59:60 in EST. Adding 1 seconds to 2008-12-31 18:59:59 should give you 2008-12-31 19:00:00 but it does not: php > echo date('c', strtotime('2008-12-31 18:59:59 + 2 seconds')); 2008-12-31T19:00:01-05:00 PHP also doesn't correctly account for the leap second in the difference between the times: php > echo strtotime('2009-01-01 00:00:00') - strtotime('2008-12-31 00:00:00'); 86400 Although it's moot since the DateTime library doesn't respect the leap second either: php > $a = new DateTime('2009-01-01 00:00:00 EST'); php > $b = new DateTime('2008-12-31 00:00:00 EST'); php > $c = $a->diff($b); php > echo $c->format('%d days, %h hours, %m minutes, %s seconds'); 1 days, 0 hours, 0 minutes, 0 seconds What you really have to know is that computer scientists didn't have anything to do with writing the calendar. The exact size of a day, month, and year change (and the number of days in a week have been altered legislatively, but not in the last 300 years). You can be relatively certain that assuming there are 86400 seconds in a day, but you need to know it's not exactly right. The reason I posted in the first place is because the OP thought 30 days from a date in February was the same day of the month in March. It's not. There are 4 values for "length of a month in days" and unfortunately two values for "length of a day," even though the leap second isn't really all that important. -Dan along with the link that you shared earlier, this is a very interesting read. I had not known about the "leap second". thank you for the information. Quote Link to comment https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313405 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.