Get past week dates from unix timestamp

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
shaddai
Forum Newbie
Posts: 4
Joined: Fri May 01, 2009 2:33 pm

Get past week dates from unix timestamp

Post 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 :banghead: :banghead: :banghead: 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Get past week dates from unix timestamp

Post by requinix »

What did you find for the current week?
shaddai
Forum Newbie
Posts: 4
Joined: Fri May 01, 2009 2:33 pm

Re: Get past week dates from unix timestamp

Post 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";
?>
 
Post Reply