Adding minute's to time()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CodingFounder
Forum Newbie
Posts: 1
Joined: Sun May 13, 2012 11:25 am

Adding minute's to time()

Post by CodingFounder »

Would this be correct?

Code: Select all

//add 2 minutes to time
$time = time() =(60 * 60 * 2)

I don't want to include it in my script and have wrong calculation's.. I think that is correct but not 100% sure.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding minute's to time()

Post by requinix »

Before the meat of my reply, first the math: you'll be adding hours because you multiplied by 60 twice. Seconds * 60 = minutes, minutes * 60 = hours.

Not to make this unintentionally complicated, but there are three different situations in which you'd use two different methods.

1. You want an amount of time measured in seconds.
A. Add seconds.

Code: Select all

$intwominutes = time() + 60 * 2;
This is because the math deals with seconds too.

2. You want an amount of time measured in minutes, hours, days, or more (weeks, months, years, etc).
B. Use strtotime().

Code: Select all

$intwodays = strtotime("+2 days");
This is because there is not always 60 seconds in a minute, 3600 (60*60) seconds in an hour, or 86400 (60*60*24) seconds in a day. Generally yes, sometimes no. If you want to deal with seconds like the math requires you to do then you'd have to take into account those exceptions. Luckily PHP/the operating system can do that for you, so let it.

3. You like friendly-looking code.
B. Use strtotime() because not only will it always be 100% correct but it's quite easy to read and understand what it's doing.

The problem is that normal people don't think much about daylight savings or leap seconds but computers have to, and if you're working with a computer then you have to too. Am I being too precise? Maybe. But did you know that June 30th, 2012 will be 86401 seconds long? Most people don't care about that difference but there are people who do. Or the more common examples: some days are 23 hours or 25 hours long (during daylight savings changes) and some years have 365 days or 366 days (during leap years).
Post Reply