Jump to content

PHP Date Operations, Getting all the dates between Jan 20 to Feb 3!


Yika

Recommended Posts

Hello All!

I have a problem of understanding how I should get the dates in between to set dates. For example, I want to get all the dates listed from Feb 21 to March 08, or maybe Dec 29 to Jan 04, so on and so forth. Im just not sure how I can do this logically with PHP.

I have no coding yet, because I have NO idea how to approach this.. so perhaps someone can help me out, or maybe there is already a script for this?? Years wouldn't matter a bit.

Im thinking I'll need to have Counters, and If statements, but please help T-T[u][/u]
[code]$feb21 = mktime(0, 0, 0, 2, 21, date("Y"));
$mar08 = mktime(0, 0, 0, 3, 8, date("Y"));

while($time=$feb21; $time<=$mar08; $time = $time+(60*60*24)){
  print date("F jS, Y", $time);
}[/code]

(not tested)
You want to use the function [url=http://www.php.net/strtotime]strtotime()[/url] to get the UNIX time stamp for the start and end of the range. Use a "for" loop, starting at the start date time stamp, going until it's greater than the ending time stamp, incrementing by 86400 (the number of seconds in a day).

Something like this:
[code]<?php
function list_dates($start,$end) {
  $ret = array();
  $sts = strtotime($start);
  $ets = strtotime($end);
  if ($ets < $sts) return (false);
  for($i=$sts;$i<=$ets;$i+=86400)
      $ret[] = $i;
  return($ret);
}
$dates = list_dates('January 20, 2007','February 3,2007');
if (is_array($dates))
  foreach ($dates as $dt)
    echo date('l, F j, Y',$dt) . '<br>';
?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.