3

I want to get the unix time stamp for that day exactly 30days back from current day. What is the best method?

Can i use this to get the date 30 days back, is this the best method?

$day = date('Y-m-d', strtotime('-30 days'));

a google search brings me to mktime() function in php. But how do i combine both and get the unix time stamp for the day? What is the easiest and fastest method?

esafwan
  • 17,311
  • 33
  • 107
  • 166
  • 3
    strtotime('-30 days') should already give you the unix timestamp of that day – ITroubs Apr 04 '11 at 16:32
  • 4
    "Exactly one month back" and "exactly 30 days back" aren't the same thing at all! You also need to consider daylight saving time transitions... you are *actually* interested in 30 * 24 hours ago? – Jon Skeet Apr 04 '11 at 16:32
  • I am interested in 1 month back. – esafwan Apr 04 '11 at 16:35
  • Actually a day or 2 day difference doesn't make much difference. I want this for an api call so i get stats from that date, and what i want is the stats for last 1 month from the given date(approx). – esafwan Apr 04 '11 at 16:40
  • one month back is the same for any date between 1st and 28th and from 28th to 31st he hase to make a definition. i usually define that for 28th to 31st the current date +/- 1 month is the next/previous month and if that month has more days than the current day then you take the current day and if the next month has less days than the current day then i take the max days of that month. – ITroubs Apr 04 '11 at 16:41
  • @esafwan then look at the question i posted as an answere. the OP had the same problem. – ITroubs Apr 04 '11 at 16:42
  • `strtotime` returns a timestamp, so what are you asking exactly? – Gordon Apr 04 '11 at 16:42

3 Answers3

3

You just need to use the strtotime("-1 month"); function. That will return a UNIX timestamp.

Nick
  • 6,967
  • 2
  • 34
  • 56
  • 1
    haha that's exactly what won't work!! try date("Y-m-d",strtotime("2011-03-31 -1 month")); should give you the last day of the last month BUT you get "2011-03-02" instead! – ITroubs Apr 04 '11 at 16:34
  • 1
    `echo " – Nick Apr 04 '11 at 16:36
1
$date = date_create();
date_sub($date, date_interval_create_from_date_string('1 m'));
echo date_format($date, 'U');
John Giotta
  • 16,432
  • 7
  • 52
  • 82
  • you could also give the first parameter to `date_create()` as the output from `mktime()` to demonstrate arbitrary dates :) – Nick Apr 04 '11 at 16:38
0

How to get previous month and year relative to today, using strtotime and date? watch this question. there is the discussion about the funny strtotime("-1 month"); bug.

Community
  • 1
  • 1
ITroubs
  • 11,094
  • 4
  • 27
  • 25
  • it is not a bug. it is expected behavior since it's using http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html#SEC120 – Gordon Apr 04 '11 at 16:45
  • well actually i wouldn't expect -1 month to stay in the same month! they may have "noticed" it somewhere but that doesn't mean it is expected to be right. – ITroubs Apr 04 '11 at 16:51