check if two dates are a week apart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ageorghiou
Forum Newbie
Posts: 12
Joined: Fri Oct 28, 2005 6:25 am

check if two dates are a week apart

Post by ageorghiou »

I'm trying to setup a simple text database with each item on a new line with the date at the start of the line. Basically when you go to the page it will find the current date and only output the items which are within one week of today. I'll have a big file with all of the items but the first time someone access the page each day it'll output the week's items into a smaller file. Probably not the best way to do this but it doesn't need to be fancy so I'm not getting too into it. Anyways, I'm trying to set up a query for determining if two dates are a week apart and I'm having some trouble.

The easiest option I decided is:

Code: Select all

list($day, $mon, $year) = split('[\/]', $date);
$itemDate= mktime(0,0,0,$mon,$day,$year);
if(date("z", $itemDate)-date("z") < 7)
{
blah blah
}
This works for me but when I get up to december I imagine it will get a bit wonked because z will reset at January 1st. Can I use the "U" option in the date() to determine if there are 604800 seconds (i think I did the math right.. after I remembered there were 60 seconds in a minute and not 30) or less between two dates? I've written this to see what I get and I keep getting an output of 0 and nothing else:

Code: Select all

echo date("U") . " ----- " .  date("U", $itemDate) . " -----" . date("U",$itemDate)-date("U") . "<br>";
Any idea what I've screwed up or whether there's a better way? The other idea I figured I could try was to check if the two dates are in the same year then do the query as above, if the item date is in next year (ie. today dec 28, 2006 news item jan 1, 2007) then check that differently and if one item is in 2006 and the other in 2008 then of course it's more than a week apart. I image using the seconds from the epoch seem a cleaner way though.

Thanks
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

it would be better to deal with timestamps

Code: Select all

list($day, $mon, $year) = split('[\/]', $date);

$itemDate = mktime(0,0,0,$mon,$day,$year);

$lastWeek = time() - 604800; // 604800 = number of second in a week

if($itemDate > $lastWeek)
{
   // item is less than a week old
}
I think that is what your were trying to do.

You dont need to worry about years and stuff youing the above method
ageorghiou
Forum Newbie
Posts: 12
Joined: Fri Oct 28, 2005 6:25 am

Post by ageorghiou »

Thanks Pimptastic, you're a star.
I'll actually make it something like:

Code: Select all

list($day, $mon, $year) = split('[\/]', $date);

$itemDate = mktime(0,0,0,$mon,$day,$year);

$nextWeek = time() + 604800;  // 604800 = number of second in a week

if($itemDate < $nextWeek)
{
   // item is within the current week
}
keep on pimpin'.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Pimptastic wrote:

Code: Select all

$lastWeek = time() - 604800; // 604800 = number of second in a week
If your country is like the UK and changes the clocks a couple of times a year (British Summer Time), you'll need to make sure the times you're using don't change.

EG

The time between 23rd March 2006 00:00:00 and 30th March 2006 00:00:00 in the UK is 1 week + 1 hour because the clocks went forward an hour on the 26th.

mktime() has an optional parameter at the end for dealing with this.
ageorghiou
Forum Newbie
Posts: 12
Joined: Fri Oct 28, 2005 6:25 am

Post by ageorghiou »

Thanks, that's good to know. I'll think about tweaking it. I don't think it's vital for this script. If you get 8 days worth of news during that week it may not really be too big a deal. I'll check the mktime() though for that.

Thanks
Post Reply