Jump to content


Photo

strtotime() vs mktime()


  • Please log in to reply
3 replies to this topic

#1 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 26 March 2006 - 07:11 PM

what is the benefit to using one over the other?

EDIT: *** BUMP *** You Dont Have to Read My Long Explaination
JUST ANSWER THE ABOVE QUESTION. THANK YOU!

I built a calendar function that lets you insert repeating dates.

it takes the date you enter, uses strtotime to conver the date to unix epoch, then adds however many seconds there are between that date and the next occurance. Thus allowing me to calculate an unlimited number of recurring events.

the problem is, that if say today is april 1 and I set an event to occur and then add (60*60*24) seconds to that number it should give me the same time of the next day. Unfortunetly, April 2nd is daylight savings, so my event is now off by an hour.

my solution, is going to be to generate that future unix date, convert it BACk to an actual date and then use that date w/ mktime() to once again create the unix time - presumably accounting for DayLight Savings.

its a messy hack, but I think it will work. Any better ideas are certainly welcome!

so my question is, should I use mktime() or strtotime() to convert a known date to unix epoch time?

Thanks!
"Confidence in the face of risk."

#2 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 27 March 2006 - 01:30 PM

*** BUMP ***

You Dont Have to Read My Long Explaination.
JUST ANSWER THE ABOVE QUESTION. THANK YOU!
"Confidence in the face of risk."

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 March 2006 - 01:49 PM

[!--quoteo(post=358875:date=Mar 27 2006, 08:30 AM:name=micah1701)--][div class=\'quotetop\']QUOTE(micah1701 @ Mar 27 2006, 08:30 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
*** BUMP ***

You Dont Have to Read My Long Explaination.
JUST ANSWER THE ABOVE QUESTION. THANK YOU!
[/quote]

don't know about most people, but my biggest difference is that strtotime() allows me to do simple arithmetic operations on the time. also, you can allow a user to input a time as a string, and you can use that as an argument for strtotime(). mktime() on the other hand allows you to specify very specific time and date with no room for logic errors. with strtotime(), if i haven't put in my code exactly right, it will still attempt to create a valid time, and it may be off, but with mktime(), if you leave something out, it will bark at you.

here is one of the biggest things to consider:
// getting the UNIX ts for tomorrow:

// with strtotime()
echo strtotime("tomorrow");

// with mktime()
echo mktime(0,0,0,date('m'), date('j') + 1, date('Y'));

they will both be accurate, but the strtotime() is MUCH easier to write and figure out later what you were trying to do. basically, i like to decide at each usage which one fits the solution best.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 27 March 2006 - 02:34 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]i like to decide at each usage which one fits the solution best.[/quote]

good call - and thats what I just ended up doing.

I convert the user dates entered from the form easily with strtotime.
when the script needs to update the calendar events, I'm using mktime().

in my calendar example, I was using just strtotime() to get a time stamp for an event, then I would add seconds to it to make it a future date. As I mentioned, this messed up the time when crossing Day Light Savings.

my solution is to use mktime($h,$m,0,$month, $day+$NUMBER_OF_DAYS_IN_FUTURE,$year)
that way mktime calcs for DLS. much better!

Thanks again for your input
"Confidence in the face of risk."




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users