Forum Moderators: coopster

Message Too Old, No Replies

strtotime alternative

         

mehh

8:53 pm on Jan 30, 2008 (gmt 0)

10+ Year Member



I have a string containing a date and I would like to convert it to a timestamp. As it has a set structure I'd rather not use strtotime to avoid bugs. Idealy I would like something that works like a reverse date() eg
fstrtotime($dateString,"d/m/Y H:i:s");

Is there such a thing?

coopster

9:04 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yeah, it's called strtotime().

;)

What is the "set structure" you are referring to? strtotime accommodates many formats. Will mktime [php.net] work better for you?

mehh

5:10 pm on Jan 31, 2008 (gmt 0)

10+ Year Member



mktime()
is from interger rather than strings. And its because
strtotime()
accepts many formats that I'd rather not use it. Take this example:
I have a string containing a date lets say 01/02/2008. I know this is in the format dd/mm/yyyy but there is no way of telling strtotime this so it could parse it mm/dd/yyyy creating a mess of the rest of my code. Thats why I'm looking for an alternitive

coopster

6:01 pm on Jan 31, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



mktime() is from integer rather than strings.

Right, you would need to parse out the pieces in order to pass them individually to the function in order to make your time.

I have a string containing a date lets say 01/02/2008. I know this is in the format dd/mm/yyyy but there is no way of telling strtotime this so it could parse it mm/dd/yyyy creating a mess of the rest of my code.

mm/dd/yyyy is a common U.S. writing and you are correct, strtotime will parse it as such. That is why I asked about your set structure. If you know the date will always come in as dd/mm/yyyy all you need to do is parse the pieces and build your timestamp from there.

mehh

3:57 pm on Feb 1, 2008 (gmt 0)

10+ Year Member



ok. I was hopeing for a built in function, but thanks for your help