Welcome to WebmasterWorld Guest from 23.22.182.29

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Simple math? Simple PHP? How to subtract a day.

It may be simple but I'm at a loss.

     
9:11 pm on Feb 25, 2011 (gmt 0)

New User

joined:Feb 25, 2011
posts:7
votes: 0


This little snippet displays Sunday's date like this:

The sunday that just happened was 2011_02_20.

<?php
$timestamp = time();
$todayday = date("w", $timestamp);
$sundayoffset = 60 * 60 * 24 * $todayday;
$lastsunday = $timestamp - $sundayoffset;
$filename = date("Y_m_d", $lastsunday) . ".";
echo("The sunday that just happened was " . $filename);

?>


Here's my question: How do I display MONDAY'S date instead of Sunday's?

Thanks a million, guys and gals. I've been away from Webmaster World too long and I'm looking to get back in gear here..
9:43 pm on Feb 25, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 11, 2006
posts:481
votes: 0


Hi,

There's a useful function called strtotime [php.net] that can do this for you:
$lastmonday = strtotime('last monday');


Andrew
11:08 am on Feb 28, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 15, 2004
posts:941
votes: 0


date('w') is the numerical representation of week days (0=SUnday, 6=Saturday)

Check what is your $todayday (0-6) and you know in what day you are
7:01 pm on Mar 2, 2011 (gmt 0)

New User

joined:Feb 25, 2011
posts: 7
votes: 0


Thanks guys!
10:13 pm on Mar 2, 2011 (gmt 0)

New User

joined:Feb 25, 2011
posts: 7
votes: 0


Like this?

<?php
$timestamp = time();
$todayday = date("2", $timestamp);
$sundayoffset = 60 * 60 * 24 * $todayday;
$lastsunday = $timestamp - $sundayoffset;
$filename = date("Y_m_d", $lastsunday) . ".";
echo("The Monday that just happened was " . $filename);

?>
11:04 pm on Mar 2, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


Works for me :)

Cheers,
MRb
4:59 pm on Mar 7, 2011 (gmt 0)

New User

joined:Feb 25, 2011
posts: 7
votes: 0


Sorry yall...

I tried that and it seemed to work, but doesn't work the following week.

@omoutop, can you eleborate please? Maybe paste in the section you're referring to? Thanks!
5:32 pm on Mar 8, 2011 (gmt 0)

New User

joined:Feb 25, 2011
posts: 7
votes: 0


Where do I put the "numerical representation of week days (0=SUnday, 6=Saturday)" in the following bit of code to make it Monday instead of Sunday? Thanks!

<?php
$timestamp = time();
$todayday = date("w", $timestamp);
$sundayoffset = 60 * 60 * 24 * $todayday;
$lastsunday = $timestamp - $sundayoffset;
$filename = date("Y_m_d", $lastsunday) . ".";
echo("The sunday that just happened was " . $filename);

?>
10:59 pm on Mar 8, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:July 13, 2010
posts:129
votes: 0


Hi sampson123,

I'm not sure if I've understood correctly but if I wanted to do what I think your asking I'd do:

<?php
$timestamp = time();
$mondayoffset = 60 * 60 * 24;
$lastmonday = $timestamp - $mondayoffset;
$filename = date("Y_m_d", $lastmonday) . ".";
echo("The monday that just happened was " . $filename);
?>

1 - get the current timestamp
2 - do the offset sum
3 - minus the offset off the time
4 - echo out the new timestamp in date format.

If you want to go back in the week you just need to change the 24 to 48 or 72 etc.

And if you want to go forward change the minus to a plus.
$lastmonday = $timestamp + $mondayoffset;

Hope it helps.
5:29 pm on Mar 11, 2011 (gmt 0)

New User

joined:Feb 25, 2011
posts: 7
votes: 0


Thanks for the reply, Orangutang. Hmm.. Although that outputs: "The monday that just happened was 2011_03_10." and that ain't right.

The 10th was a thursday, not Monday.
5:45 pm on Mar 11, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:July 13, 2010
posts:129
votes: 0


Hi Sampson,

Then I mis-understood, I wasn't sure if I had. I was just looking at what you posted, copy and pasted it and started playing around with it to try to learn something myself. When I arrived at what I posted I looked at your question again, saw it had been going on a bit and thought it was worth a post.

Just trying to help mate. :-)
6:17 pm on Mar 11, 2011 (gmt 0)

New User

joined:Feb 25, 2011
posts: 7
votes: 0


And I appreciate it!

I'd like to think I'm pretty darn decent at programming, but this takes a lobe of my brain that just doesn't make an sense to me! I can't for the life of me figure this out...
7:23 pm on Mar 11, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:July 13, 2010
posts:129
votes: 0


I know what you mean, I'm trying to learn php and what I've learnt so far is there's loads and loads to learn. :-)

You've got me now, there's this bit of code that I couldn't get my head round:

$todayday = date("w", $timestamp);

And I couldn't understand this:

$sundayoffset = 60 * 60 * 24 * $todayday;

Your multiplying 60 * 60 * 24 which gives you a day in seconds then you want to multiply that by, and there's where I got lost.

$timestamp = time();

gives you the time in seconds from a benchmarked time.

timestamp = seconds from benchmark to now.
60 * 60 * 24 = an offset in seconds.

Then as I say I couldn't get my head round.

$todayday = date("w", $timestamp)

I'm mis-understanding what your trying to do.

Is it you want to find out the time is then offset is by something by checking something else as omoutop posted.

Check what is your $todayday (0-6) and you know in what day you are.

date('w') this gives you a numerical value of today - friday = 5

if $todayday = date('w') then offset to something.

That's where I'm up to anyway :-)

edit reason was change, timestamp = seconds from benchmark to now.
9:20 pm on Mar 11, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:July 13, 2010
posts:129
votes: 0


Deleted.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members