...

View Full Version : mktime(0,0,0,date("m")+1 issue...



babelfish
03-31-2010, 05:14 PM
hi peeps im having issues with:

mktime(0,0,0,date("m")+1,date("d"),date("Y"));

the problem im seeing is that +1 on the month is going to the month after next. since today is the 31st it seems to fall over as next month doesnt have 31 days so it skips to may!

im using the following script lazily copy and pasted 12 times for a 12 month calendar (was a rush job so i never got round to sticking this as a function and looping properly)

basically this makes a holiday calendar type of thing

thanks to anyone that can help. btw this worked fine yesterday so its definately an issue with the 31st. i hate using dates in php lol



$date = time ();
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
$first_day = mktime(0,0,0,$month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
switch($day_of_week) {
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}

$days_in_month = cal_days_in_month(0, $month, $year);

$data = "<div class='tableheadNoLink'>" . strtoupper($title) . " $year</div>\n";
$data .= "<div class='layouttable'>\n";
$data .= "<table class='listtable' cellspacing='0'>\n";
$data .= "<tr>\n<th>Sunday</th>\n<th>Monday</th>\n<th>Tuesday</th>\n<th>Wednesday</th>\n<th>Thursday</th>\n<th>Friday</th>\n<th>Saturday</th>\n</tr>\n";

$day_count = 1;

$data .= "<tr>";

while ( $blank > 0 ) {
$data .= "<td valign='top' style='padding:0'> &nbsp; </td>\n";
$blank = $blank-1;
$day_count++;
}

$day_num = 1;
$today = date('Y-m-d');
while ($day_num <= $days_in_month) {

if(strlen($day_num) < 2) {
$day_num2 = "0" . $day_num;
$thisdate = "$year-$month-$day_num2";
} else {
$thisdate = "$year-$month-$day_num";
}

if($thisdate != $today) {
$highlight = "";
} else {
$highlight = " style='background-color:#f8981d; text-align:center; color:#FFFFFF'";
}

$data .= "<td valign='top' style='padding:0'><div$highlight><strong>&nbsp;$day_num&nbsp;</strong></div>";

$query = "SELECT id, trading_name, night_out, jobRefPicker, number_workers, date_worked FROM subcontractor_timesheets WHERE date_worked='$year-$month-$day_num' ORDER BY trading_name";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

if($row['night_out'] == "1") {
$appr = "<div class='night' id='timesheet{$row['id']}'>";
} else {
$appr = "<div class='day' id='timesheet{$row['id']}'>";
}

$abbname = substr($row['trading_name'] , 0, 11);
$abbproject = explode(" - ", $row['jobRefPicker']);
$abbprojectdesc = strtolower($abbproject[1]);
$abbprojectdesc = ucwords($abbprojectdesc);
$abbprojectfull = $abbproject[0] . "<br />" . substr($abbprojectdesc , 0, 14);

$popup = "<div class='timesheet_popup' id='popup{$row['id']}'>";
$popup .= "<p><strong>Subcontractor:</strong><br />{$row['trading_name']}</p>";
$popup .= "<p><strong>Date:</strong><br />" . writeDate($row['date_worked']) . "</p>";
$popup .= "<p><strong>Number of men:</strong><br />{$row['number_workers']}</p>";
$popup .= "<p><strong>Project:</strong><br />{$abbproject[0]}<br />$abbproject[1]</p>";
$popup .= "<p><a href=\"javascript:deleteSCTimesheet('{$row['id']}')\">Delete timesheet</a></p>";
$popup .= "<p><a href=\"javascript:hideObject('popup{$row['id']}')\">Close popup</a></p>";
$popup .= "</div>";

$data .= "$popup$appr<a href=\"javascript:showObject('popup{$row['id']}')\">" . $abbname . " (" . $row['number_workers'] . ")<br />";
$data .= "$abbprojectfull";
$data .= "</a></div>";
}

$data .= "</td>\n";
$day_num++;
$day_count++;

if ($day_count > 7) {
$data .= "</tr>\n<tr>\n";
$day_count = 1;
}
}

while ( $day_count >1 && $day_count <=7 ) {
$data .= "<td valign='top' style='padding:0'> &nbsp; </td>\n";
$day_count++;
}

$data .= "</tr>\n</table>\n</div collapse>\n<br />";
echo $data;

$date = mktime(0,0,0,date("m")+1,date("d"),date("Y"));
then loops....

tomws
03-31-2010, 05:25 PM
the problem im seeing is that +1 on the month is going to the month after next. since today is the 31st it seems to fall over as next month doesnt have 31 days so it skips to may!

One solution would be to use checkdate (http://php.net/checkdate) to determine whether the date is real.

babelfish
03-31-2010, 05:28 PM
i thought it would +1 month though. so it is actually +1 month with the respective day. i guess i could set the next month to day 1 of the month seeing as that would be fine.

i will look into checkdate thanks!

Goldfish
03-31-2010, 05:29 PM
Isnīt that obvious?

We are in march, so
date("m")+1 = 04

mktime(.... 04,31 ....) = may 1

If you want to make a step from last of one month to last of next month, you have to calculate the amount of days that month has, and use

date("d") + days;

babelfish
03-31-2010, 05:33 PM
lmao. this was so easy to fix in the end!

$date = mktime(0,0,0,date("m")+1,date(1),date("Y")); so it gets day1 of the next month.

sorry to waste you time. move on nothing to see here ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum