e1seix Posted April 20, 2010 Share Posted April 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199191-strtotime-1-day-with-a-variable/ Share on other sites More sharing options...
taquitosensei Posted April 21, 2010 Share Posted April 21, 2010 This should do it. echo date("Y-m-d", strtotime($_GET['search'],"+1 Days")); Quote Link to comment https://forums.phpfreaks.com/topic/199191-strtotime-1-day-with-a-variable/#findComment-1045516 Share on other sites More sharing options...
e1seix Posted April 21, 2010 Author Share Posted April 21, 2010 Oooh, thank you. However... it doesn't appear to work fully. It just outputs the $_GET["search"] value, not the -1 days part. any more ideas anyone? Quote Link to comment https://forums.phpfreaks.com/topic/199191-strtotime-1-day-with-a-variable/#findComment-1046130 Share on other sites More sharing options...
salathe Posted April 21, 2010 Share Posted April 21, 2010 Check the PHP manual for how to use strtotime, that should hopefully clarify why taquitosensei's code does not work as expected: http://php.net/strtotime Quote Link to comment https://forums.phpfreaks.com/topic/199191-strtotime-1-day-with-a-variable/#findComment-1046155 Share on other sites More sharing options...
e1seix Posted April 22, 2010 Author Share Posted April 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199191-strtotime-1-day-with-a-variable/#findComment-1046744 Share on other sites More sharing options...
oni-kun Posted April 23, 2010 Share Posted April 23, 2010 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)); Quote Link to comment https://forums.phpfreaks.com/topic/199191-strtotime-1-day-with-a-variable/#findComment-1046753 Share on other sites More sharing options...
salathe Posted April 23, 2010 Share Posted April 23, 2010 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")); Quote Link to comment https://forums.phpfreaks.com/topic/199191-strtotime-1-day-with-a-variable/#findComment-1046918 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.