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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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));

 

 

Link to comment
Share on other sites

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"));

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.