Jump to content

strtotime -1 day, with a variable


e1seix

Recommended Posts

loving the strtotime function in php, however i need to use it in association with a variable.

 

as opposed to:

 

echo date( "Y-m-d", strtotime('-1 day') );

 

let's say I already have a date defined as a variable such as

 

echo date( $_GET["search"], strtotime('-1 day') );

 

the $_GET["search"] option already being defined as "Y-m-d"

 

Any ideas, greatly appreciated.

OK, so if I use

 

echo date( "Y-m-d", strtotime( $GET["search"] ) - strtotime('-1 day') );

 

... it results in a date alright, problem is if $_GET["search"] is = "2010-04-23" it echoes "1929-09-12".

 

Likewise if I change it to:

 

echo date( "Y-m-d", time( $GET["search"] ) - strtotime('-1 day') );

 

...it echoes "1970-01-02".

 

So now it appears to be a problem with my variable definers. I'm on the right track, but I still need help. All help would be appreciated.

time( $GET["search"] ) is invalid, obviously. 1970-01-01 00:00:00 is unix epoch. Sorry, I just just woke up, a bit rusty.

 

print date( "Y-m-d", (strtotime('2010-04-23') -(time() - strtotime('-1 day')) ));

 

strtotime('-1 day') = time() - 1 day (86400 seconds)

 

Therefor you're left with a result that is, 1/1000 the integer of time(), and an incorrect date. -1 days doesn't actually do much to help you, eh.

 

This should work better:

echo date( "Y-m-d", (strtotime('2010-04-23') - 86400));

 

 

echo date( "Y-m-d", strtotime( $GET["search"] ) - strtotime('-1 day') );

 

You're getting wires crossed somewhere, the intention was to guide you towards passing a string like "2010-04-13 -1 day" into strtotime. The equivalent code more specific for you would be:

 

echo date("Y-m-d", strtotime($_GET["search"] . " -1 day"));

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.