R
E
S
O
U
R
C
E
S
       Home      Products & Services      Contact Us      Links


WebHatchers will design & develop your site for you.
_______________________

Website Menu Heaven: menus, buttons, etc.
_______________________

Send us your questions.
_______________________

site search by freefind
_______________________

HOME
SEO, Google, Privacy
   and Anonymity
Browser Insanity
JavaScript
Popups and Tooltips
Free Website Search
HTML Form Creator
Animation
Buttons and Menus
Counters
Captchas
Image Uploading
CSS and HTML
PHP
AJAX
XPATH
Website Poll
IM and Texting
Databases—MySQL
   or Not MySQL
Personal Status Boards
Content Management
   Systems
Article Content
   Management Systems
Website Directory
   CMS Systems
Photo Gallery CMS
Forum CMS
Blog CMS
Customer Records
   Management CMS
Address Book CMS
Private Messaging CMS
Chat Room CMS
JavaScript Charts
   and Graphs




Free Personal Status Boards (PSB™)

Free Standard Free PSB

Free PSB Pro Version

Free Social PSB

Free Social PSB Plus (with Email)

Free Business PSB

Free Business PSB Plus (with Email)

PSB demo

Social PSB demo

Business PSB demo

So what's all this PSB stuff about?

Chart comparing business status boards

PSB hosting diagram

PSB Licence Agreement



Copyright © 2002 -
MCS Investments, Inc. sitemap

PSBs, social networking, social evolution, microcommunities, personal status boards
PSBs, social networking, business personal status boards
website design, ecommerce solutions
website menus, buttons, image rotators
Ez-Architect, home design software
the magic carpet and the cement wall, children's adventure book
the squirrel valley railroad, model railroad videos, model train dvds
the deep rock railroad, model railroad videos, model train dvds

PHP Script: get a year of dates starting a certain number of days from a given date

This script is called get-a-year-of-dates-starting-a-certain-number-of-days-from-a-given-date.php


In searching the Internet, we found no evidence of a PHP script illustrating getting a year's worth of dates into an array, starting from a certain date or starting from a certain amount of days from a given date. In the script below, we do just that. To get a year's worth of dates into an array, starting from a certain date, make $d=0 in the second line and change the date in the first line to the date to start from. To get a year's worth of dates into an array, starting from a certain amount of days from a given date, make $d=the number of days from the given date to begin the year (in the second line) and change the date in the first line to the date to start from.

The PHP DateTime class has the methods we need to manipulate dates as needed. The DateTime::__construct method returns a new DateTime object. The DateTime::add method adds an amount of days, months, years, hours, minutes and seconds to a DateTime object. And the DateTime::format method returns a date formatted according to given format.

In the script, in the for loop put in 364 if you want a year of dates in the array, 6 in place of 364 if you want a week, 29 in place of 364 if you want 30 days—you get the idea. And you replace our sample date 02/19/2011 with your chosen starting date. And replace $d=7 with $d=0 if you want the array to start on the given date, or $d=365 if you want it to start in a year—you get the idea.

We define a $dates() array, then create a new DateTime object with the date we stick in $dates[0]. The inserted date used slash separators, but we format it for hyphens for the array. We use the date_add() method on the date along with the date_interval_create_from_date_string() function to add days to the date we created. In the example, we wanted the year of dates (that will go in the array) to begin a week after our sample date 02/19/2011, so we put 7 in $d. But as soon as the for loop is started, $d gets a 1 and it stays a 1. This increments the $date object by 1 per loop after that initial 1-week offset. Every loop, the date, formatted with hyphen separators, gets dumped into the $dates() array. There are plenty of other things you can choose to do with the script below as a starting place, or with using the DateTime objects and methods here in scripts you conjure up yourself. There is a JavaScript script inside Edit Scheduler that does similar date handling, and a PHP version in Replace This Week With Next Week that uses the array values to update a MySQL table.

This script is called get-a-year-of-dates-starting-a-certain-number-of-days-from-a-given-date.php


<?php

//get a year of dates starting a certain number of days from a given date

$dates=array();$dates[0]="02/19/2011";
$da=$dates[0];$d=7;
$date = new DateTime($da);
$da= $date->format('m-d-Y');
for($i = 0; $i <= 364; $i++){
date_add($date, date_interval_create_from_date_string($d.' days'));
$datee= $date->format('m-d-Y');
$dates[$i]=$datee;$d=1;
echo $dates[$i]."<BR>";}

?>