How can I limit this date or print this date from the star_date to end_date?
ex.
$start_date="2011-05-15";//june 15 2011
$end_date="2011-07-30";//july -7,2011
The result should be.
$dates[]="2011-05-15";
$dates[]="2011-05-16";
$dates[]="2011-05-17";
$dates[]="2011-05-18";
$dates[]="....";
$dates[]="....";
$dates[]="....";
$dates[]="....";
until it reaches.
$dates[]="2011-07-30";
I would suggest take the start date as an object and keep adding 1 day (http://www.php.net/manual/en/datetime.add.php) in a loop until you reach the end date.
<?php
$start_date = new DateTime('2011-05-15');
$end_date = new DateTime('2011-07-30');
while($end_date > $start_date)
{
echo $start_date->format('Y-m-d') . "\n";
$start_date->add(new DateInterval('P1D'));
}
?>
The above code has not been tested.
This could do it ...
$start_date = strtotime('2011-05-15'); //june 15 2011
$end_date = strtotime('2011-07-30'); //july -7,2011
$dates = array();
for ($i=$start_date; $i<=$end_date; $i+=86400) {
$dates[] = date('Y-m-d',$i);
}
$start_date="2011-05-15";
$end_date="2011-07-30";
$date=$start_date;
while (strtotime($new_date) != strtotime($end_date))
{
echo $new_date=date("Y-m-d",strtotime("+1 day", strtotime($date)))."<br>";
$dates[]=$new_date;
$date=$new_date;
}
And another....
date_default_timezone_set('America/Los_Angeles');
$startDate='2011-05-15';
$endDate='2011-07-30';
$t1=strtotime($startDate);
$days=(strtotime($endDate)-$t1)/86400;
for($i=0;$i<=$days;$i++) $dates[]=date('Y-m-d',$t1+($i*86400));
print_r($dates);
And for the 'not very efficient but will do in most cases oneliner':
for($t=strtotime($startDate);$t<=strtotime($endDate);$t+=86400) $dates[]=date('Y-m-d',$t);
And one for those who know, the last for this mornings exercises:
$dates=array_map(create_function('$t','return date("Y-m-d",$t);'),range(strtotime($startDate),strtotime($endDate),86400));
This works:
$start_date = "2011-05-15";
$end_date = "2011-07-07";
$dates = array();
$stop = strtotime($end_date);
for($i = strtotime($start_date); $i <= $stop; $i += 86400)
$dates[] = date('Y-m-d', $i);
PS. I changed your July date to 07-07 to match up with your comment.
$start_date = strtotime('2011-05-15');
$end_date = strtotime('2011-07-30');
$dates = array();
for ($i = $start_date; $i<=$end_date; $i+= (strtotime('+1 day') - strtotime('now'))) {
$dates[] = date('Y-m-d',$i);
}
Related
I have this code that modify date in the way I want, for example, if starting date is 31/01/2000, adding 1 month will return 29/02/2000, then, 31/03/2000.
If date is 30/01/2000 (not last day of the month) it will return 29/02/2000, then 30/03/2000, 30/04/2000 and so on.
I want to transfirm that code in a general case function, to be able to add 1,3,6,12 months, and inside the for loop, to work all corect.
I would like it to be a function 2 or 3 arguments, startingDate, duration(nr of iterations), frequency (add 1/3/6/12 months per once).
<?php
$date = new DateTime('2000-01-28'); // or whatever
#echo $date->format('d')." ".$date->format('t');
$expectedDay = $date->format('d');
$month = $date->format('m');
$year = $date->format('Y');
for ($i = 0; $i < 100; $i++) {
echo $date->format('Y-m-d') . "<br>";
if ($month++ == 12) {
$year++;
$month = 1;
}
$date->modify("${year}-${month}-1");
if ($expectedDay > $date->format('t')) {
$day = $date->format('t');
} else {
$day = $expectedDay;
}
$date->modify("${year}-${month}-${day}");
}
Whelp, there's an extremely easy function for this in PHP nowadays.
first, you get a timestamp instead of a datetime:
$timestamp = $date->getTimestamp();
Now, just use strtotime to add onto the date.
strtotime("+1 month", $myTimestamp);
You can change the +1 into anything you want, so you just throw the amount in the string and voila; a dynamic way of adding them!
however, since you want to do +30 days instead of a natural month, you're better off just adding 30 days to the timestamp, like so:
$timestamp = $timestamp + (3600 * 24 * 30); //s per h * h per d * d
So, you'd end up with something like this:
function calculateTime($startingDate, $iterations, $frequency){
$timeStamp = strtotime($startingDate);//if you expect a string date
$timeToAdd = (3600 * 24 * 30) * $frequency; //30 days * frequency
$return = array();
$return[] = date('Y-m-d', $timeStamp); //Original date
$previousDate = $timeStamp; //Original date for now
for($i = 0; $i < $iterations; $i++){
$newDate = $previousDate + (3600 * 24 * 30);
$return[] = date('Y-m-d', $newDate);
$previousDate = $newDate;
}
return $return;
}
And then for the rendering part:
//Let's render this stuff
$dates = calculateTime('24-08-2017', 25, 3);
foreach($dates as $date){
echo "$date</br>";
}
If you'd like to do it with full months, something like this:
<?php
function calculateTime($startingDate, $iterations, $frequency){
$timeStamp = strtotime($startingDate);//if you expect a string date
$return = array();
$return[] = date('Y-m-d', $timeStamp); //Original date
$previousDate = $timeStamp; //Original date for now
for($i = 0; $i < $iterations; $i++){
$lastDay = false;
//It's the last day of the month
if(date('t', $timeStamp) == date('d', $timeStamp)){
$lastDay = true;
}
if($frequency == 12){
$newDate = strtotime('+1 year', $previousDate);
}
else{
if($lastDay){
$firstDayOfMonth = strtotime(date("01-m-Y", $previousDate));
$newDate =strtotime("+$frequency month", $firstDayOfMonth);
}
else{
$newDate = strtotime("+$frequency month", $previousDate);
}
}
if($lastDay){
$return[] = date('Y-m-t', $newDate);
}
else{
$return[] = date('Y-m-d', $newDate);
}
$previousDate = $newDate;
}
return $return;
}
//Let's render this stuff
$dates = calculateTime('31-01-2000', 25, 1);
foreach($dates as $date){
echo "$date</br>";
}
I hope this helps? :)
If you'd like to see how this works quickly, just paste my code into a phpfiddle. Unfortunately the save function is broken right now.
I have a period with startdate of 2016-12-26 and end date 2017-03-04.
Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):
2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4
How can I accomplish this cleanest way? I have tried to:
first looking at the period_start, get the days = 26 and
find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)
then finally calculating the 4 days in 2017-03.
But is there any cleaner/better way?
This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.
$start = new DateTime("2016-12-26");
$end = new DateTime("2017-03-04");
echo $start->diff($end)->days; // Output: 68
Live demo
http://php.net/datetime.construct
#Karem hope this logic will help you, this is working case for all your conditions please try this below one:
<?php
$startDate = '2016-12-26';
$endDate = '2017-03-04';
$varDate = $startDate;
while($varDate < $endDate){
$d = date('d', strtotime($varDate));
$Y = date('Y', strtotime($varDate));
$m = date('m', strtotime($varDate));
$days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
$time = strtotime($varDate);
if($varDate == $startDate){
$time = strtotime(date('Y-m-01', $time));
$days = $days - $d;
}
else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate))){
$days = date("j", strtotime($endDate));
}
echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
$varDate = date('Y-m-d', strtotime("+1 month", $time));
}
This is long but easy to understand that how to achieve you your goal
<?php
function getMonthDays($start,$end){
if($start < $end){
$start_time = strtotime($start);
$last_day_of_start = strtotime(date("Y-m-t",$start_time));
$start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
echo date("Y-m",$start_time).": ".$start_month_days."\n";
$days = "";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
while($start < $end){
$month = date("m",$start_time);
$year = date("Y",$start_time);
$days = date('t', mktime(0, 0, 0, $month, 1, $year));
echo date("Y-m",$start_time).": ".$days."\n";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
}
echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."\n";
}else{
echo "Wrong Input";
}
}
getMonthDays('2016-12-26','2017-03-04');
?>
live demo : https://eval.in/781724
Function returns array : https://eval.in/781741
<?php
$d1 = strtotime('2016-12-26');
$d2 = strtotime('2017-03-04');
echo floor(($d2 - $d1)/(60*60*24));
?>
i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.
$startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
$endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;
$diffInMonths = $endDate->diffInMonths($startDate);
for ($i = 0; $i <= $diffInMonths; $i++) {
$start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
$end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();
echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;
}
I am getting the starting and ending dates from a form.
I need to put within an array all the date between the former two, including themselves.
I'm using a normal for loop and, at the same time, printing the result, to verify.
Everything appear to be alright.
But when I print_r the very array, I get only a series of equal dates. Which are all the same: last date + 1.
This is the code:
$date1 = date_create("2013-03-15");
$date2 = date_create("2013-03-22");
$diff = date_diff($date1, $date2);
echo $diff->format("%R%a days");
$diffDays = $diff->days;
echo $diffDays;
$dates = array();
$addDay = $date1;
for ($i = 0; $i < $diffDays; $i++) {
$dates[$i] = $addDay;
date_add($addDay, date_interval_create_from_date_string("1 day"));
echo "array: " . $i . " : " . date_format($dates[$i], 'Y-m-d');
}
print_r($dates);
PHP code demo
<?php
$dates = array();
$datetime1 = new DateTime("2013-03-15");
$datetime2 = new DateTime("2013-03-22");
$interval = $datetime1->diff($datetime2);
$days = (int) $interval->format('%R%a');
$currentTimestamp = $datetime1->getTimestamp();
$dates[] = date("Y-m-d H:i:s", $currentTimestamp);
for ($x = 0; $x < $days; $x++)
{
$currentTimestamp = strtotime("+1 day", $currentTimestamp);
$dates[] = date("Y-m-d H:i:s", $currentTimestamp);
}
print_r($dates);
I would do it that way
$startDate = new \DateTime("2017-03-15");
$endDate = new \DateTime("2017-03-22");
$dates = [];
$stop = false;
$date = $startDate;
while(!$stop){
$dates[] = $date->format('Y-m-d'); // or $dates[] = $date->format('Y-m-d H:i:s')
$date->modify('+1 day');
if($date->getTimestamp() > $endDate->getTimestamp()){
$stop = true;
}
}
print_r($dates);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generate incrementing date strings
I have:
$start_date = '2012-09-03';
$number_days = 5;
I would like receive array with this dates:
$dates = array(
'2012-09-03',
'2012-09-04',
'2012-09-05',
'2012-09-06',
'2012-09-07'
);
What is the best way for this?
$start_date = '2012-09-03';
$dates[] = $start_date;
$number_days = 5;
for ($i=1; $i < $number_days; $i++) {
$dates[] = date('Y-m-d', strtotime("$start_date +$i days"));
}
http://php.net/manual/en/function.date-add.php
Loop over and add the dates you need.
Have you tried something like this
function get_days($start_date, $max){
$ts=strtotime($start_date);
$next_day_interval=24*60*60;
$arr=array();
$arr[]=$start_date;
for($i=1;$i<=$max;$i++){
$ts += $next_day_interval;
$arr[]=date('Y-m-d', $ts);
}
return $arr;
}
Just wrote it here so there might be some compile time errors but I hope you get the idea.
Here's what you're searching for (work also on PHP < 5.3)
<?php
$start_date = '2012-09-03';
$number_days = 5;
$stdate = date(strtotime($start_date));
$dates = array();
for($i = 0 ; $i < $number_days ; $i++) {
$dates[$i] = date('Y-m-d', $stdate) ;
$stdate += 24*60*60;
}
var_dump($dates);
?>
Try this.
$start_date = '2012-09-03';
$number_days = 5;
$dates = array();
$TS = strtotime($start_date);
$dates[0] = $start_date;
for($i=1;$i<5;$i++)
{
$dates[$i] = date('Y-m-d', strtotime('+1 day', $TS));
$TS = strtotime($dates[$i]);
}
Modified Code from #vinay to print actual output
<?php
$start_date = '2012-09-03';
$number_days = 5;
$dates = array();
$TS = strtotime($start_date);
for($i=0;$i<5;$i++)
{
$dates[$i] = date('Y-m-d', strtotime('+1 day', $TS));
$TS = strtotime($dates[$i]);
echo date('y-m-d',$TS).'<br>';
}
?>
Here is simple example with PHP 5.3 DateTime and DateInterval. This is clear solution. Note: PHP 5.2 supports DateTime, but not DateInterval. You may declare it in custom class in PHP 5.2, see here: DateInterval Definition.
<?php
$start_date = '2012-09-03';
$number_days = 5;
$dt = new DateTime($start_date);
$dates = array();
for($i = 0; $i < $number_days; $i++) {
$dates[] = $dt->format("Y-m-d");
$dt->add(new DateInterval("P1D"));
}
print_r($dates);
?>
I am trying to populate a select list with time.
I want to create the select list so that it starts from the starting date and then ends six months later.
I've created this for loop for now but it doesn't work:
$dateSelectList = '';
$startDate = $c->getStartDate(92);
$endDate = intval( strtotime('+6 month', $startdate) );
$i = 1;
$tempDate = 0;
for($date = $startdate; $date <= $endDate ; $date = strtotime('+1 day', $date))
{
$dateSelectList .= '<option id="select'.$i.'" value="'.$date.'">'.$date.'</option>';
$i++;
}
$dateSelectList .= '</select>';
I think it's the last field in the for loop but I don't know how to get around it.
I've changed it to $date = strtotime('+1 day', $date) and it works now.
Thanks a lot !
In each iteration, you're resetting the date to the start date plus one day. I.e., you're just using the same date over and over each iteration:
for($date = $startdate; $date <= $endDate ; $date = strtotime('+1 day', $startdate))
Change your for loop so that it keeps adding on to $date instead:
for($date = $startdate; $date <= $endDate ; $date = strtotime('+1 day', $date))
There are plenty of solutions. One of them may be:
Code
$startdate = time(); // today;
$enddate = strtotime('+6 months', $startdate);
while ($startdate <= $enddate) {
echo date('Y-m-d', $startdate) . "<br/>";
$startdate = strtotime('+1 day', $startdate);
}
Output
2012-03-26
2012-03-27
2012-03-28
2012-03-29
2012-03-30
2012-03-31
2012-04-01
...
2012-09-24
2012-09-25
2012-09-26
Now, modify code and create your selector as you like.
Change first line to
$year = 2012;
$month = 3;
$day = 26;
$startdate = strtotime("$year-$month-$day 00:00:00 UTC");
and create your custom $startdate.
Complete selector code
$year = 2012;
$month = 2;
$day = 3;
$startdate = strtotime("$year-$month-$day 00:00:00 UTC");
$enddate = strtotime('+6 months', $startdate);
$doc = "<select>"; $i=1;
while ($startdate <= $enddate) {
$dt = date('Y-m-d', $startdate);
$doc .= "<option id=\"select$i\" value=\"$dt\">$dt</option>";
$startdate = strtotime('+1 day', $startdate);
$i++;
}
$doc .= "</select>";
echo $doc;
Output
More elegant solution is to put it all into function like this
function createSelector($day, $month, $year) {
$startdate = strtotime("$year-$month-$day 00:00:00 UTC");
$enddate = strtotime('+6 months', $startdate);
$doc = "<select>"; $i=1;
while ($startdate <= $enddate) {
$dt = date('Y-m-d', $startdate);
$doc .= "<option id=\"select$i\" value=\"$dt\">$dt</option>";
$startdate = strtotime('+1 day', $startdate);
$i++;
}
$doc .= "</select>";
return $doc;
}
and call it this way
$selectorCode = createSelector(26, 3, 2012);
echo $selectorCode;
Cheers!
The problem is, indeed, with this bit of code: $date = strtotime('+1 day', $startdate) ...
$startdate is never being changed, therefore, $date is never being changed. You'll want something more like $date = strtotime('+1 day', $date) in order for the loop to work properly.