Webmaster Forum



Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2008, 10:13 AM
T0d's Avatar
T0d T0d is offline
v7n Mentor
Latest Blog:
None

 
Join Date: 01-11-04
Location: Sacramento
Posts: 1,559
iTrader: 0 / 0%
T0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest order
Exclamation PHP - Loop through dates

I'm wondering the most efficient/fastest way to loop through two given dates and pull-out each respective date.
IE: User inputs: 11/25/07 and 12/25/07
I want to go through and pull out each day in between.

MKTIME? What would you do and why.

-Todd

Last edited by T0d; 02-09-2008 at 10:30 AM.
Share |
Reply With Quote
  #2 (permalink)  
Old 02-09-2008, 10:46 AM
T0d's Avatar
T0d T0d is offline
v7n Mentor
Latest Blog:
None

 
Join Date: 01-11-04
Location: Sacramento
Posts: 1,559
iTrader: 0 / 0%
T0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest order
Here's what I have it seems pretty sloppy though.


PHP Code:
$year1 '2007';
$month1 '11';
$day1 '25';

$year2 '2007';
$month2 '12';
$day2 '25';

    
$start_date "$year1-$month1-$day1";
    
$end_date    "$year2-$month2-$day2";
    
    
$date mktime(0,0,0,$month1,$day1,$year1); //Gets Unix timestamp START DATE
    
$date1 mktime(0,0,0,$month2,$day2,$year2); //Gets Unix timestamp END DATE
    
$difference $date1-$date//Calcuates Difference
    
$daysago floor($difference /60/60/24); //Calculates Days Old

    
echo "Total Days Between: ".$daysago;

$i 0;
while (
$i <= $daysago +1) {
    if (
$i != 0) { $date $date 86400; }
    else { 
$date $date 86400; }
    
$today date('Y-m-d',$date);
    echo 
"$i) Day: $today <br>";
    
$i++;

Share |
Reply With Quote
  #3 (permalink)  
Old 02-10-2008, 02:49 PM
lordspace's Avatar
Contributing Member
 
Join Date: 05-30-06
Location: Canada
Posts: 899
iTrader: 0 / 0%
lordspace is a name known to alllordspace is a name known to alllordspace is a name known to alllordspace is a name known to alllordspace is a name known to alllordspace is a name known to alllordspace is a name known to alllordspace is a name known to alllordspace is a name known to alllordspace is a name known to alllordspace is a name known to all
Send a message via ICQ to lordspace Send a message via Skype™ to lordspace
Cool. Looks nice.
Share |
Reply With Quote
  #4 (permalink)  
Old 02-11-2008, 11:21 AM
T0d's Avatar
T0d T0d is offline
v7n Mentor
Latest Blog:
None

 
Join Date: 01-11-04
Location: Sacramento
Posts: 1,559
iTrader: 0 / 0%
T0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest orderT0d is a web professional of the highest order
As it turned out in my program I didn't need to actually do the -# and a few other minor changes, works though.
__________________
Web development specialist.
Share |
Reply With Quote
  #5 (permalink)  
Old 04-04-2008, 01:30 PM
Junior Member
 
Join Date: 04-04-08
Location: BC, Canada
Posts: 12
iTrader: 0 / 0%
catch22media is liked by many
I stumbled upon this thread looking to do the same thing and although this thread is a bit old, but I thought I'd drop in my two cents.

I was able to accomplish the same thing like this:

PHP Code:
$start_date '2008-03-01';
$check_date $start_date;
$end_date '2008-03-14';

while (
$check_date != $end_date) {
    
$check_date date ("Y-m-d"strtotime ("+1 day"strtotime($check_date)));
    echo 
$check_date '<br>';

You'll probably want to build some error checking into this so you don't create an infinite loop, something like the following would work:

PHP Code:
$start_date '2008-03-01';
$check_date $start_date;
$end_date '2008-03-14';

$i 0;

while (
$check_date != $end_date) {
    
$check_date date ("Y-m-d"strtotime ("+1 day"strtotime($check_date)));
    echo 
$check_date '<br>';

    
$i++;
    if (
$i 31) { die ('Error!'); }

From here you would use $check_date to do what you want. Of course you can format these dates anyway you wish thanks to the wonderful php date() function.

Luke
Share |
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)
 
Thread Tools
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Loop Help, Please JohnRogers Coding Forum 1 01-21-2007 11:27 AM
Getting Back into the Loop IslaScotts Coding Forum 4 09-28-2006 01:05 PM
Loop Javascript Immo Coding Forum 5 09-20-2004 04:04 AM
Infinite Loop hatchet Coding Forum 1 09-11-2004 04:45 PM


Sponsor Links
Get exposure! Contextual Links V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 02:16 AM.
Powered by vBulletin
Copyright © 2000-2011 Jelsoft Enterprises Limited.
Copyright © 2003 - 2011 Escalate Media LP




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.