note 74241 added to function.mktime

From: Date: Sat, 31 Mar 2007 16:46:06 +0000
Subject: note 74241 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-124975@lists.php.net to get a copy of this message
You cannot simply subtract or add month VARs using mktime to obtain previous or next months as suggested in previous user comments (at least not with a DD > 28 anyway). If the date is 03-31-2007, the following yeilds March as a previous month. Not what you wanted. <?php $dateMinusOneMonth = mktime(0, 0, 0, (3-1), 31, 2007 ); $lastmonth = date("n | F", $dateMinusOneMonth); echo $lastmonth; //---> 3 | March ?> mktime correctly gives you back the 3rd of March if you subtract 1 month from March 31 (there are only 28 days in Feb 07). If you are just looking to do month and year arithmetic using mktime, you can use general days like 1 or 28 to do stuff like this: <?php $d_daysinmonth = date('t', mktime(0,0,0,$myMonth,1,$myYear)); // how many days in month $d_year = date('Y', mktime(0,0,0,$myMonth,1,$myYear)); // year $d_isleapyear = date('L', mktime(0,0,0,$myMonth,1,$myYear)); // is YYYY a leapyear? $d_firstdow = date('w', mktime(0,0,0,$myMonth,'1',$myYear)); // FIRST falls on what day of week (0-6) $d_firstname = date('l', mktime(0,0,0,$myMonth,'1',$myYear)); // FIRST falls on what day of week Full Name $d_month = date('n', mktime(0,0,0,$myMonth,28,$myYear)); // month of year (1-12) $d_monthname = date('F', mktime(0,0,0,$myMonth,28,$myYear)); // Month Long name (July) $d_month_previous = date('n', mktime(0,0,0,($myMonth-1),28,$myYear)); // PREVIOUS month of year (1-12) $d_monthname_previous = date('F', mktime(0,0,0,($myMonth-1),28,$myYear)); // PREVIOUS Month Long name (July) $d_month_next = date('n', mktime(0,0,0,($myMonth+1),28,$myYear)); // NEXT month of year (1-12) $d_monthname_next = date('F', mktime(0,0,0,($myMonth+1),28,$myYear)); // NEXT Month Long name (July) $d_year_previous = date('Y', mktime(0,0,0,$myMonth,28,($myYear-1))); // PREVIOUS year $d_year_next = date('Y', mktime(0,0,0,$myMonth,28,($myYear+1))); // NEXT year $d_weeksleft = (52 - $d_weekofyear); // how many weeks left in year $d_daysinyear = $d_isleapyear ? 366 : 365; // set correct days in year for leap years $d_daysleft = ($d_daysinyear - $d_dayofyear); // how many days left in year ?> ---- Server IP: 216.194.113.175 Probable Submitter: 70.112.103.231 ---- Manual Page -- http://www.php.net/manual/en/function.mktime.php Edit -- https://master.php.net/note/edit/74241 Del: integrated -- https://master.php.net/note/delete/74241/integrated Del: useless -- https://master.php.net/note/delete/74241/useless Del: bad code -- https://master.php.net/note/delete/74241/bad+code Del: spam -- https://master.php.net/note/delete/74241/spam Del: non-english -- https://master.php.net/note/delete/74241/non-english Del: in docs -- https://master.php.net/note/delete/74241/in+docs Del: other reasons-- https://master.php.net/note/delete/74241 Reject -- https://master.php.net/note/reject/74241 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124975) next »