Jump to content

strtotime() vs mktime()


micah1701

Recommended Posts

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!
Link to comment
Share on other sites

[!--quoteo(post=358875:date=Mar 27 2006, 08:30 AM:name=micah1701)--][div class=\'quotetop\']QUOTE(micah1701 @ Mar 27 2006, 08:30 AM) [snapback]358875[/snapback][/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:
[code]
// 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'));
[/code]

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.
Link to comment
Share on other sites

[!--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
Link to comment
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.