note 51354 added to function.date

From: Date: Mon, 28 Mar 2005 18:49:00 +0000
Subject: note 51354 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-87097@lists.php.net to get a copy of this message
Some experiments to find out first and last day of a week. For sure not the best way, but a working one :) <?php $day = date ("j"); $month = date ("n"); $year = date ("Y"); (int)$dayofweek = date("w", mktime (1,1,1,date("n"),date("j"),date("Y"))); $dayofweek = ($dayofweek == 0 ? 6 : $dayofweek-1); (int)$thisweek = date("W", mktime(0,0,1,$month,$day,$year)); (int)$today = date("j", mktime(0,0,1,$month,$day,$year)); (int)$thismonthdays = date("t", mktime(0,0,1,$month,$day,$year)); (int)$lastmonthdays = date("t", mktime(0,0,1,$month-1,1,$year)); (int)$lastmonth = date("n", mktime(0,0,1,$month-1,1,$year)); (int)$nextmonth = date("n", mktime(23,59,59,$month+1,1,$year)); $firstweekday = ($dayofweek == 0 ? $today : ($today-$dayofweek<=0 ? $lastmonthdays-($dayofweek-$today) : $today-$dayofweek)); $lastweekday = ($dayofweek == 6 ? $today : ($today+(6-$dayofweek) > $thismonthdays ? (6-$dayofweek)-($thismonthdays-$today) : $today+(6-$dayofweek))); $fwd_month = ($today-$dayofweek <= 0 ? $lastmonth : date("n")); $fwd_year = ($fwd_month==$lastmonth && $fwd_month == 12 ? date("Y")-1 : date("Y")); $lwd_month = ($today+(6-$dayofweek) > $thismonthdays ? $nextmonth : date("n")); $lwd_year = ($lwd_month==$nextmonth && $lwd_month == 1 ? date("Y")+1 : date("Y")); echo "Today: ".date("D d.M.Y"); echo "<br>".++$dayofweek.". day of $thisweek. week"; echo "<br>First day of week: ".date("D d.M.Y", mktime(0,0,1,$fwd_month,$firstweekday,$fwd_year)); echo "<br>Last day of week: ".date("D d.M.Y", mktime(23,59,59,$lwd_month,$lastweekday,$fwd_year)); ?> ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51354 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51354&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51354&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51354&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51354&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51354&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51354&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#87097) next »