Jump to content


Photo

date increment by one day


  • Please log in to reply
14 replies to this topic

#1 hotliquer

hotliquer
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 04 April 2006 - 07:49 PM

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 04 April 2006 - 08:03 PM

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:
<?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
}
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 djnrempel

djnrempel
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 04 April 2006 - 08:05 PM

The way I have incremented dates is to get the unix timestamp of the first date, then increment it by the number of seconds in one day, and then use the date function to convert it back to Y-m-d or whatever.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 April 2006 - 08:06 PM

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:
<?
$first_date = $_GET['first_date'];
$next_date = date("Y-m-d", strtotime("+1 day",strtotime($first_date)));
echo $next_date;
?>

Ken


#5 hotliquer

hotliquer
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 04 April 2006 - 08:31 PM

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

#6 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 04 April 2006 - 08:37 PM

$now = time();
$tomorow = date("Y-m-d",$now+(60*60*24));

[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 April 2006 - 08:41 PM

To the OP, can you post the code you are currently are debugging?

Ken

#8 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 04 April 2006 - 08:56 PM

$first_date = $_GET['first_date'];
$next_date = date("Y-m-d", strtotime($first_date)+(60*60*24));
echo $next_date;

Is what your looking for.
You was calling strtotime an extra time.
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#9 hotliquer

hotliquer
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 04 April 2006 - 09:19 PM

[!--quoteo(post=361726:date=Apr 4 2006, 03:41 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 4 2006, 03:41 PM) View Post[/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.

$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><?

I cannot post the whole script!? it not to big as i checked. just keep getting error page?

#10 hotliquer

hotliquer
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 04 April 2006 - 09:32 PM

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

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

#11 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 04 April 2006 - 09:48 PM

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


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

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.
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 April 2006 - 10:10 PM

This code gives the correct answer:
<?php
$first_date = $_GET['first_date'];
$second_date = $_GET['second_date'];
$increment = date("Y-m-d", strtotime("+1 day",strtotime($first_date)));
?>
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

#13 hotliquer

hotliquer
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 04 April 2006 - 10:31 PM

[!--quoteo(post=361755:date=Apr 4 2006, 05:10 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 4 2006, 05:10 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
This code gives the correct answer:
<?php
$first_date = $_GET['first_date'];
$second_date = $_GET['second_date'];
$increment = date("Y-m-d", strtotime("+1 day",strtotime($first_date)));
?>
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??


#14 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 04 April 2006 - 10:35 PM

Did you try mine?

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


Theres absolutely no reason that should not work...
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#15 hotliquer

hotliquer
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 04 April 2006 - 10:47 PM

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

$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;
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:
http://www.bristol-glass.co.uk/production/employee_prod_stats_test.php


its currently coded to your code, DrDre




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users