Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a month in timestamp in each iteration

Tags:

php

Here is my PHP Code that generate a selectbox with month names + Year. I have 2 Time Stamps that I am using in the loop that start timestamp and end timestamp.

now the problem is for example if my start-timestamp represent 01.07.2011 and as in the loop I am adding only 30 Days to time stamp then i selectbox July will be displayed twice. and if I add 31 Day then may be a month will be skiped.

Is there any function available that can add exact one month for each iteration?

<select name="checkinMonth" class="selectform" id="checkinMonth" >
<?php
   for($month = $checkin_timestamp; $month <= $checkout_timestamp; $month += 30*60*60*24) {
      $checkin_month = getdate($month);
      $option_text =  strftime("%B %Y",$month);
      $option_value =  strftime("%Y%m", $month);
      $selected = ($checkin_selected == $option_value ? "selected='selected'" : "");

         echo "<option value='{$option_value}' {$selected}>{$option_text}</option>";
    }
?>
</select> 
like image 728
user160820 Avatar asked Jul 01 '11 15:07

user160820


2 Answers

Are you looking for something like:

strtotime("+1 month", $myTimestamp); 

http://us2.php.net/strtotime

like image 175
Dalton Conley Avatar answered Oct 22 '22 02:10

Dalton Conley


And if you fancy something more verbose, try this:

$tmp_year = date("Y", $month);
$tmp_month = date("m", $month);
$tmp_day = date("d", $month);
$tmp_hour = date("H", $month);
$tmp_min = date("m", $month);
$tmp_sec = date("s", $month);

$month = mktime($tmp_hour, $tmp_minute, $tmp_second, $tmp_month + 1, $tmp_day, $tmp_year);

I guess you'll have to put it in its own function to make it fit in that for-clause :P

like image 41
jous Avatar answered Oct 22 '22 02:10

jous