Removing days in loop php - php

I have this simple PHP code
$date_start = date("Y-m-d 00:00:00");
$date_end = date("Y-m-d 23:59:59");
$i = 0;
while($i < 900):
print_r($date_start);
$i++;
$date_start = date($date_start, strtotime("-$i days"));
$date_end = date($date_end, strtotime("-$i days"));
print_r($date_start);
echo $i."<br>";
endwhile;
I would like to get date today then, yesterday, then the date before that and so on. I have tried several variations of this, but I can't seem to be able to get it to work. Any help would be appriciated

It can be done, please have a look on the below code it may help you.
<?php
$i = 0;
$date_start = date('Y-m-d');
while($i < 10):
$days_ago = date('Y-m-d', strtotime("-$i days", strtotime($date_start)));
$i++;
echo $days_ago;
echo "<p></p>";
endwhile;
?>

try this
$i = 0;
while($i < 900):
$i++;
$date_start = date("Y-m-d 00:00:00", strtotime("-$i days"));
$date_end = date("Y-m-d 23:59:59", strtotime("-$i days"));
echo $i." ".$date_start." ".$date_end."\n";
endwhile;

Related

Calculate days in months between two dates with PHP

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;
}

Inserting dates within an array with PHP

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);

PHP: get last 6 months in format month year

Is there a way in PHP to get the current and the previous 5 months in the following format?
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
Have you tried following:
<?php
echo date('F, Y');
for ($i = 1; $i < 6; $i++) {
echo date(', F Y', strtotime("-$i month"));
}
Let me know, if this wont work.
Do not use:
<?php
for ($i = 0; $i <= 6; $i++) {
echo date('F Y', strtotime(-$i . 'month'));
}
// With date e.g.: "May, 31", outputs:
// May, 2018, May 2018, March 2018, March 2018, January 2018, December 2017
You can fix it by:
<?php
for ($i = 0; $i <= 6; $i++) {
echo date('F Y', strtotime('last day of ' . -$i . 'month'));
}
Or better use DateTime, e.g.:
$dateTime = new DateTime('first day of this month');
for ($i = 1; $i <= 6; $i++) {
echo $dateTime->format('F Y');
$dateTime->modify('-1 month');
}
Try this
for ($j = 0; $j <= 5; $j++) {
echo date("F Y", strtotime(" -$j month"));
}
Why not use DateTime Object as
$start = new DateTime('first day of this month - 6 months');
$end = new DateTime('last month');
$interval = new DateInterval('P1M'); // http://www.php.net/manual/en/class.dateinterval.php
$date_period = new DatePeriod($start, $interval, $end);
$months = array();
foreach($date_period as $dates) {
array_push($months, $dates->format('F').' '.$dates->format('Y'));
}
print_r($months);
Use strtotime and date
for( $i = 0; $i <= 5 ; $i++) {
print date("F Y", strtotime("-".$i." month"))."\n";
}
to achieve another formats for date look PHP date format HERE
<?php
for ($i =0; $i < 6; $i++) {
$months[] = date("F Y", strtotime( date( 'Y-m-01' )." -$i months"));
}
print_r($months)
?>
Try This..
for ($i = 1; $i <= 6; $i++) {
$months[] = date("M-y", strtotime( date( 'Y-m-01' )." -$i months"));
}
print_r($months);
#php
for($i=0; $i<=5; $i++) {
$last_six_months[] =date("Y-m-d", strtotime( date( 'Y-m-01' )." -$i months"));
}
$last_six_months = array_reverse($last_six_months); // If you want to reverse...
#endphp
next >>>
#foreach($last_six_months as $key => $row)
<span>{{date('F Y',strtotime($row))}}</span>
#endforeach

Listing Dates In Order (Options Drop-Down)

I want to produce a form that has a dropdown options box containing dates in order and in a format but I have no idea how to display them as a string.
I assume that you need to do this sort of thing to get the start and finish but how can I get the dates for each "Option"?
<?php
date_default_timezone_set('UTC');
$start = date('Ymd');
$end = date('Ymd', strtotime('+60 days'));
while (strtotime($start) <= strtotime($end)) {
echo "$start\n";
$date = date ("Ymd", strtotime("+1 day", strtotime($start)));
}
?>
$dates = array();
$dates[] = time();
for ($i = 1; $i < 60; $i++) { $dates[] = strtotime("+{$i} days"); }
$html = NULL;
foreach ($dates as $date) { $dates .= "<option value='" . date('Ymd', $date) . "'>" . date('d F Y', $date) . "</option>"; }
$html = "<select id='dates'>{$dates}</select>";
echo $html;
I would just like to add to Publi Design's answer to make the answer more specific to the question.
PHP courtesy of Publi Design:
// Start date
$date = '2009-12-06';
// End date
$end_date = '2020-12-31';
?>
<select id="dates">
<?php while (strtotime($date) <= strtotime($end_date)) { ?>
<option name="date"><?php echo (string)$date; ?></option>
<?php $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}?>
</select>
Using a basic PHP while loop:
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = '2009-12-06';
// End date
$end_date = '2020-12-31';
while (strtotime($date) <= strtotime($end_date)) {
echo "$date\n";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
Original source: here
This might be what you're looking for..
<select id="dates">
<option></option>
<?php
$curr_date = date('YYMMDD');
for ($i = 0; $i < 60; $i++) {
$date = strtotime("+$i day", $curr_date);
echo '<option name="date" value="'.$date.'">'.date('DD m YY', strtotime($date)).'</option>'."\n";
}
?>
</select>

How to make a date limit in php?

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);
}

Categories