Jump to content

date increment by one day


hotliquer

Recommended Posts

Hi. I am trying to increment a date by one day, that is already set in a variable ($first_date) in this format 2005-04-02

This is the closest I have got so far:

$first_date = $_GET['first_date'];
$first_date = date("Y-m-d", strtotime("+1 day"));


But this to my dismay resets $first_date to today’s current date!

I’m sure this is an easy task for many of you guys. But for me; it’s driving me bonkers!

Thanks

Ben
Link to comment
Share on other sites

you've got to check whether or not your variable is being set before you can use date/time functions on it... for instance, make sure that $_GET['first_date'] is actually being set and that it is NOT empty:
[code]
<?php
if (isset($_GET['first_date'])) {
  // variable is set
  // default to today's date if the URL variable is empty
  $first_date = !empty($_GET['first_date']) ? $_GET['first_date'] : date('Y-m-d');
  echo date('Y-m-d', strtotime("$first_date +1 day");
} else {
  // variable not set in the URL
}
?>
[/code]
Link to comment
Share on other sites

By default, strtotime() assumes the starting date is today. The second parameter to the function specifies the starting date as UNIX time.

Here's one way of doing what you want:
[code]<?
$first_date = $_GET['first_date'];
$next_date = date("Y-m-d", strtotime("+1 day",strtotime($first_date)));
echo $next_date;
?>[/code]

Ken
Link to comment
Share on other sites

Guys,

Thanks so much for your time. But I am still having the same problem.

obsidian the $_GET['first_date'] has the date in it, which is 2005-07-21.

Dan R. I will be looking into this one. I have never worked in the UNIX seconds jobby thing yet!

kenrbnsn this is working, but it takes 2005-07-21 and turns it into 2006-04-05.

Here exactly what I am doing. I have a simple form with two fields. I put in two dates i.e. $first_date 2005-07-21 and $second_date 2005-07-21. This accesses the production database with daily production stats for a bunch of glass blowers. It also pumps out what their bonus will be for that day.

So to make it easier for my boss to work out our piece work I want a link on the page, that just increments the date, from the last page, by one day.

So say I am looking at prod stats for 2005-07-21 I want to see this:
[a href=\"http://www.blah.com/employee_prod_stats.php?first_date=2005-07-22&second_date=2005-07-22\" target=\"_blank\"]http://www.blah.com/employee_prod_stats.ph...date=2005-07-22[/a]

But the closest I can get to it is:
[a href=\"http://www.blah.com/employee_prod_stats.php?first_date=2006-04-05&second_date=2006-04-05\" target=\"_blank\"]http://www.blah.com/employee_prod_stats.ph...date=2006-04-05[/a]

I really hope that makes more sense!??

Thanks a huge bunch for your help so far guys
Link to comment
Share on other sites

[!--quoteo(post=361726:date=Apr 4 2006, 03:41 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 4 2006, 03:41 PM) [snapback]361726[/snapback][/div][div class=\'quotemain\'][!--quotec--]
To the OP, can you post the code you are currently are debugging?

Ken
[/quote]

Here is the part of code i am debugging.

[code]
$first_date = $_GET['first_date'];
$first_date = $_GET['second_date'];
$increment = date("Y-m-d", strtotime("+1 day",strtotime($first_date)));

?><h4><a href="employee_prod_stats_test.php?first_date=<?php echo $increment; ?>&second_date=<?php echo $increment; ?>">Stats for next day</a></h4><?
[/code]

I cannot post the whole script!? it not to big as i checked. just keep getting error page?
Link to comment
Share on other sites

[!--quoteo(post=361728:date=Apr 4 2006, 03:56 PM:name=DrDre)--][div class=\'quotetop\']QUOTE(DrDre @ Apr 4 2006, 03:56 PM) [snapback]361728[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]$first_date = $_GET['first_date'];
$next_date = date("Y-m-d", strtotime($first_date)+(60*60*24));
echo $next_date;
[/code]

Is what your looking for.
You was calling strtotime an extra time.
[/quote]

This gives me the same problem. it turns 2005-07-21 into 2006-04-05 (tomorrow) when i want it to increment to 2005-07-22.

basically i want to increment, by one day, $first_date.

$first_date will never be today or tomorrow. it will always in the past.

thanks for your halp!

Ben
Link to comment
Share on other sites

Ok i never used strtotime, it must be as above said and uses current time.


[code]
$first_date = $_GET['first_date'];
$first_date = split('-',$first_date);
$time=mktime(0,0,0,$first_date[1],$first_date[2],$first_date[0]);
$next_date = date("Y-m-d", $time+(60*60*24));
echo $next_date;
[/code]

Splitting the _GET request by - into an array of Array(Year,Month,Day)
And call mktime(hr,min,sec,month,day,year) to find the unix timestamp and then add the 1 day of seconds to it and then back to a Y-m-d format.
Link to comment
Share on other sites

This code gives the correct answer:
[code]<?php
$first_date = $_GET['first_date'];
$second_date = $_GET['second_date'];
$increment = date("Y-m-d", strtotime("+1 day",strtotime($first_date)));
?>[/code]
I believe your problem occured because you're overlaying the variable $first_date with the value of $_GET['second_date'] in your second line.

To: DrDre, the strtotime() function will take a string and try to convert it into a unix time stamp, so you could pass it something like "July 21, 2006" and it will do the "right thing"

Take a look at the [a href=\"http://www.php.net/strtotime\" target=\"_blank\"]manual page[/a] for it.

Ken
Link to comment
Share on other sites

[!--quoteo(post=361755:date=Apr 4 2006, 05:10 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 4 2006, 05:10 PM) [snapback]361755[/snapback][/div][div class=\'quotemain\'][!--quotec--]
This code gives the correct answer:
[code]<?php
$first_date = $_GET['first_date'];
$second_date = $_GET['second_date'];
$increment = date("Y-m-d", strtotime("+1 day",strtotime($first_date)));
?>[/code]
I believe your problem occured because you're overlaying the variable $first_date with the value of $_GET['second_date'] in your second line.

To: DrDre, the strtotime() function will take a string and try to convert it into a unix time stamp, so you could pass it something like "July 21, 2006" and it will do the "right thing"

Take a look at the [a href=\"http://www.php.net/strtotime\" target=\"_blank\"]manual page[/a] for it.

Ken
[/quote]


unfortuatly i am still having the same problem. i must have other issues with the code. the dates i am looking for keep jumping to 2006-04-05. i am guessing that my code is messy or something like that??
but i know when i print $first_date it is 2005-07-21 or any other date i chose, that is in the past.

thanks for the help guys. i dont know what else to try??
Link to comment
Share on other sites

Did you try mine?

[code]$first_date = $_GET['first_date'];
$first_date = split('-',$first_date);
$time=mktime(0,0,0,$first_date[1],$first_date[2],$first_date[0]);
$next_date = date("Y-m-d", $time+(60*60*24));
echo $next_date;[/code]


Theres absolutely no reason that should not work...
Link to comment
Share on other sites

[!--quoteo(post=361760:date=Apr 4 2006, 05:35 PM:name=DrDre)--][div class=\'quotetop\']QUOTE(DrDre @ Apr 4 2006, 05:35 PM) [snapback]361760[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Did you try mine?

[code]$first_date = $_GET['first_date'];
$first_date = split('-',$first_date);
$time=mktime(0,0,0,$first_date[1],$first_date[2],$first_date[0]);
$next_date = date("Y-m-d", $time+(60*60*24));
echo $next_date;[/code]
Theres absolutely no reason that should not work...
[/quote]
yes i did try your one. and unfortunatly it turned my date into 1970-01-02

i think my code is pretty screwed. it must be the other code in the script. i am currently trying to build a stock, sales and production database for work. its in early stages so far. i would like to show some one it so they could see what the production thing is doing wrong.

i have copied it here to a folder with out pass word protection:
[code]http://www.bristol-glass.co.uk/production/employee_prod_stats_test.php[/code]

its currently coded to your code, DrDre
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.