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

Lizard
  • 43,732
  • 39
  • 106
  • 167
  • looks like i need to convert the timestamp to something like "+1 week 2 days 4 hours 2 seconds" – Lizard Dec 19 '13 at 08:07
  • if that return mm-dd-YYY hour:minute:second. then is that be ok for You requirement? – Ripa Saha Dec 19 '13 at 08:09
  • http://www.php.net/manual/en/datetime.diff.php – Hanky Panky Dec 19 '13 at 08:09
  • check http://stackoverflow.com/questions/10040291/converting-a-unix-timestamp-to-formatted-date-string – Ripa Saha Dec 19 '13 at 08: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". :) – Amal Murali Dec 19 '13 at 08:16

2 Answers2

5

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

 Unix Timestamp   "@" "-"? [0-9]+   "@1215282385"
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • And how are you so sure that the OP is using DateTime class? He said he's using `strtotime()`. – Amal Murali Dec 19 '13 at 08:08
  • @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 Dec 19 '13 at 08:10
  • @Nanne: Yes, I'm aware of that. However, I was confused myself after I first read the question and your answer. +1 – Amal Murali Dec 19 '13 at 08:13
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

exussum
  • 18,275
  • 8
  • 32
  • 65