2

I am calling an external API and one of the parameters I have to pass is the date but in a string like (now or last week) this in turn gets converted to a timestamp using strtotime.

The problem is that I already have a timestamp but the API requires a string.

So my question is how do I take a timestamp and convert it to a string that strtotime() will return the exact same timestamp?

Thanks

5
  • looks like i need to convert the timestamp to something like "+1 week 2 days 4 hours 2 seconds" Commented Dec 19, 2013 at 8:07
  • if that return mm-dd-YYY hour:minute:second. then is that be ok for You requirement? Commented Dec 19, 2013 at 8:09
  • php.net/manual/en/datetime.diff.php Commented Dec 19, 2013 at 8:09
  • check stackoverflow.com/questions/10040291/… Commented Dec 19, 2013 at 8:14
  • @ripa: That's totally different. This question is asking "how do I take a timestamp and convert it to a string that strtotime() will return the exact same timestamp?" not "how do I convert a timestamp to a formatted date string". :) Commented Dec 19, 2013 at 8:16

2 Answers 2

5

According to the manual you are allowed to provide a unix timestamp, if you precede it with an @:

 Unix Timestamp   "@" "-"? [0-9]+   "@1215282385"
Sign up to request clarification or add additional context in comments.

3 Comments

And how are you so sure that the OP is using DateTime class? He said he's using strtotime().
@AmalMurali What are you talking about? If you read the manual for strtotime() it clearly refers you to these "date and time formats" pages, as the formats it accepts. It even starts with This section describes all the different formats that the strtotime(), DateTime and date_create() parser understands.
@Nanne: Yes, I'm aware of that. However, I was confused myself after I first read the question and your answer. +1
1

you can convert using date

example

date(DATE_ISO8601, $your_timestamp);

strtotime will be able to parse the ISO8601 date back on the other side

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.