WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Function that returns an array with the 7 dates of the week that belong to the supplied date.
Categories : PHP, Date Time, Arrays
Boaz Yahav
Date : Mar 28th 2003
Grade : 3 of 5 (graded 7 times)
Viewed : 7846
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Boaz Yahav
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?
//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&lt;=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&lt;=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