note 94497 modified in function.date by danbrown

From: Date: Sun, 08 Nov 2009 20:50:43 +0000
Subject: note 94497 modified in function.date by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162653@lists.php.net to get a copy of this message
Function which simply returns numeric specification what day of week was(or will be) at given date (same schema as date("w")). Works both for future and past dates. Counting starts from 0 for Sunday and ends at 7 for Saturday. Hope it will help anybody. ;) Here's the function: <?php function return_day_of_week($date){ $sy=substr($date, 0, 4); $sm=substr($date, 5, 2); $sd=substr($date, 8, 2); $date_utc=mktime(0,0,0,$sm, $sd, $sy); $today_utc=mktime(0,0,0,date("m"), date("d"), date("Y")); if($date_utc>$today_utc){ $future_date=1; $temp=$date_utc; $date_utc=$today_utc; $today_utc=$temp; } $utc_difference=$today_utc-$date_utc; $weeks_count=($utc_difference)/604800; if($weeks_count<10) $weeks_count=substr($weeks_count, 0, 1); else if($weeks_count<100) $weeks_count=substr($weeks_count, 0, 2); else if($weeks_count<1000) $weeks_count=substr($weeks_count, 0, 3); $days_rest_count=substr(($utc_difference-$weeks_count*604800)/86400, 0, 1); $was_day_of_week=date("w")-$days_rest_count; if($was_day_of_week < 0){ if($future_date==1) $was_day_of_week=0-$was_day_of_week; else $was_day_of_week+=7; } return $was_day_of_week; } ?> --was-- Function which simply returns numeric specification what day of week was(or will be) at given date (same schema as date("w")). Works both for future and past dates. Counting starts from 0 for Sunday and ends at 7 for Saturday. Hope it will help anybody. ;) Here's the function: function return_day_of_week($date){ $sy=substr($date, 0, 4); $sm=substr($date, 5, 2); $sd=substr($date, 8, 2); $date_utc=mktime(0,0,0,$sm, $sd, $sy); $today_utc=mktime(0,0,0,date("m"), date("d"), date("Y")); if($date_utc>$today_utc){ $future_date=1; $temp=$date_utc; $date_utc=$today_utc; $today_utc=$temp; } $utc_difference=$today_utc-$date_utc; $weeks_count=($utc_difference)/604800; if($weeks_count<10) $weeks_count=substr($weeks_count, 0, 1); else if($weeks_count<100) $weeks_count=substr($weeks_count, 0, 2); else if($weeks_count<1000) $weeks_count=substr($weeks_count, 0, 3); $days_rest_count=substr(($utc_difference-$weeks_count*604800)/86400, 0, 1); $was_day_of_week=date("w")-$days_rest_count; if($was_day_of_week < 0){ if($future_date==1) $was_day_of_week=0-$was_day_of_week; else $was_day_of_week+=7; } return $was_day_of_week; } http://php.net/manual/en/function.date.php

« previous php.notes (#162653) next »