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
 
Thread Tools Display Modes
Share |
  #1  
Old 02-09-2008, 11:13 AM
T0d's Avatar
T0d T0d is offline
v7n Mentor
 
Join Date: 01-11-04
Location: Sacramento
Posts: 1,569
iTrader: 0 / 0%
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 11:30 AM.
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 02-09-2008, 11:46 AM
T0d's Avatar
T0d T0d is offline
v7n Mentor
 
Join Date: 01-11-04
Location: Sacramento
Posts: 1,569
iTrader: 0 / 0%
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++;

 
Reply With Quote
  #3  
Old 02-10-2008, 03:49 PM
lordspace's Avatar
lordspace lordspace is offline
Contributing Member
 
Join Date: 05-30-06
Location: Canada
Posts: 913
iTrader: 0 / 0%
Cool. Looks nice.
 
Reply With Quote
  #4  
Old 02-11-2008, 12:21 PM
T0d's Avatar
T0d T0d is offline
v7n Mentor
 
Join Date: 01-11-04
Location: Sacramento
Posts: 1,569
iTrader: 0 / 0%
As it turned out in my program I didn't need to actually do the -# and a few other minor changes, works though.
 
Reply With Quote
  #5  
Old 04-04-2008, 01:30 PM
catch22media catch22media is offline
Junior Member
 
Join Date: 04-04-08
Location: BC, Canada
Posts: 12
iTrader: 0 / 0%
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
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Loop Help, Please JohnRogers Coding Forum 1 01-21-2007 12:27 PM
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


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 06:31 PM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC