if i have two dates 20-4-2010 and 22-4-2010 in two text box I want the dates to be like this 20,21,22 how to get that
+7
A:
You can use mktime().
mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input.
If you increment the day number you get a valid date back, even if you go past the end of the month:
<?php
$day= 25;
$dateEnd = mktime(0,0,0,5,3,2010);
do {
$dateCur = mktime(0,0,0,4,$day,2010);
$day++;
print date( 'd-m-y', $dateCur) .'<br>';
} while ($dateCur < $dateEnd);
Output:
25-04-10
26-04-10
27-04-10
28-04-10
29-04-10
30-04-10
01-05-10
02-05-10
03-05-10
Tom Haigh
2010-04-29 11:25:44
+3
A:
I am pretty sure this has been answered a quadrillion times before, but anyway:
$start = strtotime('20-04-2010');
$end = strtotime('22-04-2010');
for($current = $start; $current <= $end; $current += 86400) {
echo date('d-m-Y', $start);
}
or by giving the number of days:
for($i = 0; $i <= 2; $i++) {
echo date('d-m-Y', strtotime("20-04-2010 +$i days"));
}
or with PHP5.3
$period = new DatePeriod(
new DateTime('20-04-2010'),
DateInterval::createFromDateString('+1 day'),
new DateTime('23-04-2010') // or pass in just the no of days: 2
);
foreach ( $period as $dt ) {
echo $dt->format( 'd-m-Y' );
}
Gordon
2010-04-29 11:28:52
+1 The Bestest Reply...
OM The Eternity
2010-04-29 12:16:32
+1 for the Date* classes, -1 for calling it "fancy" and "overkill", +1 for giving something other than the oft-quoted `strtotime` solution. :)
salathe
2010-04-29 12:31:57
@salathe that still gives +1 so Yay! :) Unfortunately, the DateInterval and DatePeriod classes are still somewhat scarcely documented. I'd use them more often if it wasn't so frustrating figuring them out. `strtotime` is just dead simple. Can't we add some of the examples of Derick's php|architect book? Or from these slides: http://derickrethans.nl/talks/time-phplondon9.pdf
Gordon
2010-04-29 12:39:10
@Gordon good to see your arithmetic is up to scratch! I believe the DateInterval and DatePeriod pages will be getting some love in the near future* so do keep an eye out. (* the DateTime class docs, particularly examples, have been revamped recently so it makes sense to move onto those others next)
salathe
2010-04-29 13:07:29
@salathe cool. Actually I thought about contributing to the docs myself, but I didn't find the time to get through http://doc.php.net/php/dochowto/ yet.
Gordon
2010-04-29 13:11:12
@Gordon feel free to get in touch with any thoughts or questions about contributing: the more the merrier. :) Certainly looks like `mktime` has won this particular battle though!
salathe
2010-04-29 13:34:55
+1
A:
You can do:
$start = strtotime("2010-04-20"); // get timestamp for start date.
$end = strtotime("2010-04-22"); // get timestamp for end date.
// go from start timestamp to end timestamp adding # of sec in a day.
for($t=$start;$t<=$end;$t+=86400) {
// get the date for this timestamp.
$d = getdate($t);
// print the date.
echo $d['mday'].'-'.$d['mon'].'-'.$d['year']."\n";
}
Output:
20-4-2010
21-4-2010
22-4-2010
codaddict
2010-04-29 11:30:20