Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/256185-adding-30-days/
Share on other sites

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  :tease-01:

Link to comment
https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313351
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313404
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/256185-adding-30-days/#findComment-1313405
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.