Page 1 of 1
Get past week dates from unix timestamp
Posted: Thu Dec 17, 2009 6:27 pm
by shaddai
I need help. I need to take a Unix timestamp from the past and determine what the previous monday was, as well as the following sunday. I've been

for a day so far. I've found 500 ways to do it for the current week, nothing for a week in the past.
Got any ideas?
Todd
Re: Get past week dates from unix timestamp
Posted: Thu Dec 17, 2009 6:33 pm
by requinix
What did you find for the current week?
Re: Get past week dates from unix timestamp
Posted: Thu Dec 17, 2009 6:49 pm
by shaddai
More than I care to talk about.
This gets you the monday just past:
Code: Select all
$monday = date('Y-m-d',strtotime('monday',time()-60*60*24*6));
This gets you sunday, referenced from the monday just past
Code: Select all
$sunday = date('Y-m-d',strtotime('monday + 6 day',time()-60*60*24*6));
So no matter what day it is, it knows where monday is for this week, and sunday for this week. I wanted to know how to get monday and sunday from the week containing the unix version of 11/10/08.
Anyway, true to form, as soon as I ask for help, I figure it out and can't believe I missed got hung up on something so simple. I'll post it here for others:
Code: Select all
<?php
# $olddate is a datetime field from mysql
$timestamp = date('U', strtotime($olddate));
echo "Unix:" . $timestamp . "\n";
$monday = strtotime("last monday", $timestamp);
$mondate = date('Y-m-d', $monday);
echo "Monday:" . $mondate . "\n";
$sunday = strtotime("last monday + 6 day", $timestamp);
$sundate = date('Y-m-d', $sunday);
echo "Sunday:" . $sundate . "\n";
?>