|
|
|
<?
//This function receives a specific date and returns the days of the week
//that the input day belongs to. The function returns an array with 7 dates
//in this format : YYYY-mm-dd.
function WeekDates($y,$m,$d){
$TimeStamp=mktime(0,0,0,$m,$d,$y);
for($x=0; $x<=6; $x++){
$DateArray[$x] = date('Y-m-d',mktime(0,0,0,date('m',$TimeStamp),date
('d',$TimeStamp)-date('w',$TimeStamp)+$x,date('Y',$TimeStamp)));
}
return $DateArray;
}
//Example 1
$Array=WeekDates("2002","12","31");
For($i = 0 ; $i <= 6 ; $i++) {
Echo "$i." . $Array[$i] . "<BR>";
}
/*
Output :
2002-12-29
2002-12-30
2002-12-31
2003-01-01
2003-01-02
2003-01-03
2003-01-04
*/
//Example 2
$Array=WeekDates("2002","2","26");
For($i = 0 ; $i <= 6 ; $i++) {
Echo "$i." . $Array[$i] . "<BR>";
}
/*
Output :
2002-02-24
2002-02-25
2002-02-26
2002-02-27
2002-02-28
2002-03-01
2002-03-02
*/
//Example 3
$Array=WeekDates("2003","1","1");
For($i = 0 ; $i <= 6 ; $i++) {
Echo "$i." . $Array[$i] . "<BR>";
}
/*
Output :
2002-12-29
2002-12-30
2002-12-31
2003-01-01
2003-01-02
2003-01-03
2003-01-04
*/
?>
|
|
| Select with current month Categories : PHP, HTML and PHP, Date Time, Arrays | | | Convert an input date to various formats (e.g. Monday, Mon, Mo, M, 1). Categories : PHP, Date Time, Arrays | | | enhanced date picker with jcript checking for a dynamic date input Categories : PHP, Java Script, Date Time, Calendar, Arrays | | | Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | | | clearing variables in php3 Categories : Variables, Arrays, PHP | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | | | Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | A wrapper function to format dates coming from a databases with the
same syntax as PHP's date() function. Categories : Date Time, Databases, PHP | | | Stock quotes from yahoo! Categories : PHP, Web Services, Arrays | | | pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes | | | Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
Categories : PHP, Directories, Variables, Arrays | | | translate.php - Assocciative array example, passing a reference to a function. Categories : PHP, Arrays, Languages, Variables | |
| | | | Fred Schenk wrote :915
Hello Yahav Boaz,
I like your improved version, especially the examples you placed with it (probably just to stop my critisizing ;-))) I just wondered...
The function is now called with the seperate element. With those you create a timestanp and from this timestamp you regenerate the year, month and day. Couldn`t you suffice with:
function WeekDates($y,$m,$d){
$Weekday=date(`w`,mktime(0,0,0,$m,$d,$y));
for($x=0; $x<=6; $x++){
$DateArray[$x] = date(`Y-m-d`,$m,$d-$Weekday+$x,$y));
}
return $DateArray;
}
Or, to maximise for speed:
function WeekDates($y,$m,$d){
$FirstDOW=$d-date(`w`,mktime(0,0,0,$m,$d,$y));
for($x=0; $x<=6; $x++){
$DateArray[$x] = date(`Y-m-d`,$m,$FirstDOW+$x,$y));
}
return $DateArray;
}
As I see from the supplied examples PHP is smart enough to understand that a date like 2003/01/-1 means 2002/12/31. Quite a relieve since your funtcion relied upon it. Now it only relies on the fact that the first day of the week on the server must be the same as the user/programmer wants it to be (do I sense another enhancement here ;-)))
Regards,
Fred
| |
|
|
|