homepage Welcome to WebmasterWorld Guest from 54.145.209.80
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Simple math? Simple PHP? How to subtract a day.
It may be simple but I'm at a loss.
sampson123



 
Msg#: 4272550 posted 9:11 pm on Feb 25, 2011 (gmt 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..

 

Little_G

5+ Year Member



 
Msg#: 4272550 posted 9:43 pm on Feb 25, 2011 (gmt 0)

Hi,

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

Andrew

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4272550 posted 11:08 am on Feb 28, 2011 (gmt 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

sampson123



 
Msg#: 4272550 posted 7:01 pm on Mar 2, 2011 (gmt 0)

Thanks guys!

sampson123



 
Msg#: 4272550 posted 10:13 pm on Mar 2, 2011 (gmt 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);

?>

Matthew1980

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4272550 posted 11:04 pm on Mar 2, 2011 (gmt 0)

Works for me :)

Cheers,
MRb

sampson123



 
Msg#: 4272550 posted 4:59 pm on Mar 7, 2011 (gmt 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!

sampson123



 
Msg#: 4272550 posted 5:32 pm on Mar 8, 2011 (gmt 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);

?>

Orangutang



 
Msg#: 4272550 posted 10:59 pm on Mar 8, 2011 (gmt 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.

sampson123



 
Msg#: 4272550 posted 5:29 pm on Mar 11, 2011 (gmt 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.

Orangutang



 
Msg#: 4272550 posted 5:45 pm on Mar 11, 2011 (gmt 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. :-)

sampson123



 
Msg#: 4272550 posted 6:17 pm on Mar 11, 2011 (gmt 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...

Orangutang



 
Msg#: 4272550 posted 7:23 pm on Mar 11, 2011 (gmt 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.

Orangutang



 
Msg#: 4272550 posted 9:20 pm on Mar 11, 2011 (gmt 0)

Deleted.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved