TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-13-2009, 07:11 PM   #1 (permalink)
The Visitor
 
Join Date: Mar 2009
Posts: 2
Thanks: 0
kawasaki is on a distinguished road
Default Getting the dates between 2 dates

I need to figure out a way to get the dates between $startdate and $enddate. Then insert all of them in to a MySQL table. This is for a small events calendar i've developed.

I've come across this function, just not sure how to grab each individual date and insert it into the table.
This is a simple snippet of code that will return an array of days between two dates.

Code
PHP Code:
<?php
function GetDays($sStartDate$sEndDate){
  
// Firstly, format the provided dates.
  // This function works best with YYYY-MM-DD
  // but other date formats will work thanks
  // to strtotime().
  
$sStartDate gmdate("Y-m-d"strtotime($sStartDate));
  
$sEndDate gmdate("Y-m-d"strtotime($sEndDate));
 
  
// Start the variable off with the start date
  
$aDays[] = $sStartDate;
 
  
// Set a 'temp' variable, sCurrentDate, with
  // the start date - before beginning the loop
  
$sCurrentDate $sStartDate;
 
  
// While the current date is less than the end date
  
while($sCurrentDate $sEndDate){
    
// Add a day to the current date
    
$sCurrentDate gmdate("Y-m-d"strtotime("+1 day"strtotime($sCurrentDate)));
 
    
// Add this new day to the aDays array
    
$aDays[] = $sCurrentDate;
  }
 
  
// Once the loop has finished, return the
  // array of days.
  
return $aDays;
}
?>
Any Ideas? This is for inserting entries into a calendar FROMdate TOdate. Know what i mean? Thank you for your suggestions.

Kawasaki@Nexuz

Last edited by kawasaki : 03-14-2009 at 03:34 PM.
kawasaki is offline  
Reply With Quote
Old 03-13-2009, 07:17 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Please use [php] or [highlight=php] tags around the phpcode. It will be easier for us to read your code then - thus making it easier for us to help you.
__________________
Tanax is offline  
Reply With Quote
Old 03-14-2009, 03:35 PM   #3 (permalink)
The Visitor
 
Join Date: Mar 2009
Posts: 2
Thanks: 0
kawasaki is on a distinguished road
Default

k, any ideas? What i want to accomplish is to be able to give a start date and end date and have it insert all of the dates from start to finish into the event table. Also Maybe if i had a startDate and then span # of days.
kawasaki is offline  
Reply With Quote
Old 03-14-2009, 03:48 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Assuming that dateA and dateB are unix timestamps representing dates where dateA < dateB, this should work:

PHP Code:
//1 day = seconds/hours in day/minutes in hours/seconds in minute)
//Floor because it will be the lowest integer
$startDay floor($dateA/24/60/60);
$endDay =  floor($dateB/24/60/60);
$daysDiff $endDay-$startDay
for($i=0;i<$daysDiff;$i++)
{
//This is the start date plus $i days.
$day Date("y-m-d",$startDay*24*60*60+$i*24*60*60)

Basically what this does is:
Convert timestamp A into days
Convert timestamp B into days
Get the difference
Starting from the first day, represent that day plus 0 to the difference.

It would, however, be more efficient to store the start date and the end date in the database and have the script (or the query) generate your dates opposed to inserting each one.
__________________

Village Idiot is offline  
Reply With Quote
Old 03-14-2009, 04:16 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

You haven't provided much information on the database structure, but wouldn't it be more efficient to store the start and end date/time on a per-event basis and deal with which days an individual item covers in the frontend code? Unless there's a reason each day must be saved individually?..
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the distance between 2 dates... may Advanced PHP Programming 18 06-08-2008 04:01 AM
Most efficient way of playing with dates? bdm Absolute Beginners 6 12-14-2007 01:36 PM
How to find difference between dates? Haris Absolute Beginners 2 10-06-2007 07:47 PM
Tutorial - Getting th, st and rd (ordinal suffixes) on numbers and dates serversphere Tips & Tricks 2 04-04-2007 10:06 AM


All times are GMT. The time now is 03:57 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design