current date minus 1 month |
current date minus 1 month |
jimlongo |
Feb 2 2009, 11:54 AM
Post
#1
|
This is My Life Group: Members Posts: 1,128 Joined: 24-August 06 From: t-dot Member No.: 16 |
Hi,
I currently have a footer function that gives the last modified date of the webpage. CODE function lmod($doc) { $doc=$_SERVER["DOCUMENT_ROOT"].$doc; return date("Y-m-d", filemtime($doc)); Client would like the date to always be the current date minus 1 month (or some period in days). I've got that the current date would be CODE function lmod($doc) { $doc=$_SERVER["DOCUMENT_ROOT"].$doc; return date("Y-m-d", filemtime($doc)); Anyone have a clue that would help me figure out how to subtract some period of days from that? Thanks in advance, jim P.S. Don't know why board inserts slashes before and after Y-m-d . . . they aren't there in my code. This post has been edited by jimlongo: Feb 2 2009, 11:59 AM |
Brian Chandler |
Feb 2 2009, 12:27 PM
Post
#2
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
This thread might help: http://forums.htmlhelp.com/index.php?showtopic=7820
I recommend using php for the date arithmetic (at least the manual is better than MySQL's...) You need to extract year, month, day from the date, then something like: $stamp = mktime(12,0,0, $nowmm-1, $nowdd, $nowyyyy); What about one month before 31st March? mktime() does all the normalisation, so it should turn into "Feb 31st", which is Mar 2 or 3. If you want to use the last day of the previous month in such cases, neatest is to use mktime(0, 0, 1, $nowmm, 1, $nowyy) to get just after midnight first day of the month, then subtract a couple of hundred seconds to get into the day before, and use that to get the date. HTH |
Brian Chandler |
Feb 2 2009, 12:29 PM
Post
#3
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Sorry: Of course first you test if the resulting month for "one month ago" is the same as this month, which catches all the "carry" cases, THEN you do...
QUOTE mktime(0, 0, 1, $nowmm, 1, $nowyy) to get just after midnight first day of the month,then subtract a couple of hundred seconds to get into the day before, and use that to get the date. This stuff is notoriously easy to get wrong. Did you hear about the million Zune[?] music thingies that just stopped working on January 1st this year? |
jimlongo |
Feb 2 2009, 01:54 PM
Post
#4
|
This is My Life Group: Members Posts: 1,128 Joined: 24-August 06 From: t-dot Member No.: 16 |
That's great, then how do I convert that Unix timestamp into Y-m-d format?
|
Brian Chandler |
Feb 2 2009, 02:11 PM
Post
#5
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Sorry, I can never remember which is which, but date() goes in one direction, time() in the other. I think it's date() which you use with a format string....
$yr = date("Y", $stamp); $mth = date("m", $stamp); $day = date("d", $stamp); Something like that. (I use my own jstdate() that adds 9 hours to get Japanese time.) |
jimlongo |
Feb 2 2009, 02:21 PM
Post
#6
|
This is My Life Group: Members Posts: 1,128 Joined: 24-August 06 From: t-dot Member No.: 16 |
I seem to have something working here . . . can you translate this to English for me?
$stamp = mktime($nowyyyy, $nowmm-40000, $nowdd); $aLMod = date('Y-m-d', $stamp); Gives a result of 2009-01-05 when today is 2009-02-02 Thanks. This post has been edited by jimlongo: Feb 2 2009, 02:22 PM |
Brian Chandler |
Feb 2 2009, 10:16 PM
Post
#7
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
I seem to have something working here . . . can you translate this to English for me? $stamp = mktime($nowyyyy, $nowmm-40000, $nowdd); $aLMod = date('Y-m-d', $stamp); Gives a result of 2009-01-05 when today is 2009-02-02 Thanks. Here's the manual: int mktime ([ int $hour=date("H") [, int $minute=date("i") [, int $second=date("s") [, int $month=date("n") [, int $day=date("j") [, int $year=date("Y") [, int $is_dst=-1 ]]]]]]] ) The arguments to mktime are in a hideously jumbled order, but you can only omit them from the right end (obviously, since that's how C family functions work). So on 2009-02-02, you've calculated the time 2009 hours, -39998 minutes, and 2 seconds on [today], and it's worked out the answer. But I can't imagine that's what you meant... http://jp.php.net/manual/en/function.mktime.php |
jimlongo |
Feb 3 2009, 11:02 AM
Post
#8
|
This is My Life Group: Members Posts: 1,128 Joined: 24-August 06 From: t-dot Member No.: 16 |
Alright, thanks.
Seem to be getting somewhere . . . this appears to be NOW minus 672 hours (28 days). Is that the case? $stamp = mktime(now-672); $aLMod = date('F j, Y', $stamp); |
Brian Chandler |
Feb 3 2009, 01:18 PM
Post
#9
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Alright, thanks. Seem to be getting somewhere . . . this appears to be NOW minus 672 hours (28 days). Is that the case? $stamp = mktime(now-672); $aLMod = date('F j, Y', $stamp); Um, no. What is "now"?* The first argument to mktime must be the hours, normally from 0 to 23, but out of range values get normalised. So if you take mktime(-672) this is -672 hours, x mins y sec, ... etc, and the rest of the values get filled in with the current date. So this gives you 672 hours before 0000 today. But if you just want to subtract a fixed time, you may as well take the timestamp (which is a count in seconds), and subtract (e.g.) 28*24*60*60 for 28 days. * 'now' isn't _anything_ in the above expression - it's not a variable - but anyway the name would be wrong, because this represents only _hours_. I thought you wanted "same day last month" ... in which case mktime() is the neat way to do it. |
jimlongo |
Feb 3 2009, 01:49 PM
Post
#10
|
This is My Life Group: Members Posts: 1,128 Joined: 24-August 06 From: t-dot Member No.: 16 |
Just some period earlier than today is sufficient. 28 days is what that seems to be (672 hours) - when I adjust the number it works in that sense.
Still works like this (without now) $stamp = mktime(-672); $aLMod = date('F j, Y', $stamp); The result is close enough for the client's request . . . they just want to make it appear as if the site has been updated "recently". For me I'd just like to understand what's going on. Thanks, jim |
Lo-Fi Version | Time is now: 19th April 2024 - 02:55 PM |