-1 day help

I’m having a hard time trying to figure out strtotime and adding and subtracting days. I have a digital logbook that I am trying to sort by day so when I log in I only see entries for that day, but I want to create links where I can flip through days.

[php]echo date(“Y-m-d”, strtotime($row_logbook[‘timestamp’])); [/php]

Where can I put -1 day or +1 day?

Any help would be appreciated. I’ve searched google endlessly and cannot figure it out.

Hi,

This is how it works:
[php]
$timestamp = date( ‘Y-m-d’ );
$timestamp_yesterday = date( ‘Y-m-d’, strtotime( $timestamp. ’ -1 day’ ) );
$timestamp_tomorrow = date( ‘Y-m-d’, strtotime( $timestamp. ’ +1 day’ ) );
[/php]

Is that what you wanted to know?

Good luck,
O.

I actually did this:

[php]<?php echo date("Y-m-d", strtotime("-1 day", strtotime($row_logbook['timestamp'])));?>[/php]

It seemed to do the trick. I’m very new with all this and dreamweaver is actually teaching me while I skim the php.net manual. The only issue I’m running into now is if there are no entries for the day I’m looking at the server throws back the default 12/31/69 date and I cannot go any further. Any suggestions on how to avoid that?

Thanks!

Hi,

you mean you want to set a default value?

[php]

<?php echo date("Y-m-d", strtotime( ( $row_logbook['timestamp'] ? $row_logbook['timestamp'] : date( 'Y-m-d' ) ) . "-1 day" )));?>

[/php]
(okay, not the most clear code, it’s a kind of shortcut for an ‘if…then…else’) This will return the date before the $row_logbook, unless it’s ‘0’, then it’ll return yesterday.

Is that what you asked?
O.

PS. To clarify
[php]
// ( CONDITION ? Result if TRUE : Result if FALSE )

$a = ‘’;
echo ( $a ? $a : ‘$a doesn’t evaluate as “TRUE”’ ); // echoes: $a doesn’t evaluate as “TRUE”
$a = ‘Whoopteedoo’;
echo ( $a ? $a : ‘$a doesn’t evaluate as “TRUE”’ ); // echoes: Whoopteedoo
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service