Jump to content

problem in date subtraction after uploading


sheraz

Recommended Posts

hi, i am working on an assignment in which i am getting current date and then getting date of two previous months i.e

 

2010-09

2010-08

2010-07

 

the problem is that the code is working perfectly on localhost but when i upload the file it only shows the current date and do not subtract and display dates. can some body guide me that what is the problem and how to solve it. :-\

 

here is the code

 

 

$date1= date("Y-m");

 

$date2 = strtotime ( '-1 month' , strtotime ( $date1 ) ) ;

$date2 = date ( 'Y-m' , $date2 );

 

$date3 = strtotime('-1 month', strtotime( $date2 ) );

$date3 = date( 'Y-m' , $date3);

?>

 

<select name="">

 

<option><?php echo $date1; ?></option>

<option><?php echo $date2; ?></option>

<option><?php echo $date3; ?></option>

 

</select>

 

 

 

Link to comment
Share on other sites

I suspect the problem is a difference between the date/time settings on your PC vs the Server. For example, in the US Jan. 5th, 2010 would be represented as 1-5-2010, whereas in most European countries it would be represented as 5-1-2010. So, I imagine your PC's settings recognizes "2010-09" as September 2010 and the settings on the Server do not.

 

But, all of that can be solved by removing the overcomplication from what you have. You are converting dates back and forth unneccessarily. Try this:

 

<select name="">
  <option><?php echo date("Y-m"); ?></option>
  <option><?php echo date("Y-m", strtotime("-1 month")); ?></option>
  <option><?php echo date("Y-m", strtotime("-2 months")); ?></option>
</select>

Link to comment
Share on other sites

Or, for a better programatical solution you could use the code below. That way you can easily modify how many options are displayed in the list by modifying the value of $numberOfMonthsToDisplay.

 

<?php

$numberOfMonthsToDisplay = 3;
$dateOptions = '';
for($monthsBack=0; $monthsBack<$numberOfMonthsToDisplay; $monthsBack++)
{
    $dateValue = date("Y-m", strtotime("-{$monthsBack} month"));
    $dateOptions .= "  <option value=\"{$dateValue}\">{$dateValue}</option>\n";
}

?>

<select name="">
  <?php echo $dateOptions; ?>
</select>

Link to comment
Share on other sites

I really like helping people, but c'mon could you not take 5 minutes to figure out the problem yourself? You need to learn how to effectively debug code if you are going to program. You didn't even provide enough information for me to reproduce the defect. You should have stated that you set the date to Jan 31st since that was the only date in January that caused the problem. And, I was easily able to discover why by just modifying the $dateValue to

$dateValue = date("Y-m-d", strtotime("-{$monthsBack} month"));

I then saw the results as

2010-01-31
2009-12-31
2009-12-01

 

I don't know if this is a PHP bug or not. But, instead of trying to fight it a simple code change would fix it. Just force the base date to be the first day of the current month.

<?php

$numberOfMonthsToDisplay = 3;
$dateOptions = '';
for($monthsBack=0; $monthsBack<$numberOfMonthsToDisplay; $monthsBack++)
{
    $firstDayOfMonth = date("Y-m-1");
    $dateValue = date("Y-m", strtotime("$firstDayOfMonth -{$monthsBack} months"));
    $dateOptions .= "  <option value=\"{$dateValue}\">{$dateValue}</option>\n";
}

?>

<select name="">
  <?php echo $dateOptions; ?>
</select>

 

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.