This question already has answers here:
How to count days between two dates in PHP? [duplicate]
(11 answers)
Closed 8 years ago.
counting no. Of days between two dates inclusive of starting date is easy but how to count them saperately for each months like between 1.1.2014 and 15.5.2014
Jan 31
Feb 28
Mar 31
Apr 30
May 15
I think you are looking for something like this:
$start = new DateTime('2014-01-01');
$end = new DateTime('2014-05-15');
$tmpStart = $start;
while ( $tmpStart <= $end ) {
$tmpEnd = clone $tmpStart;
$tmpEnd = $tmpEnd->modify('last day of this month');
$tmpEnd = $tmpEnd < $end ? $tmpEnd : clone $end;
echo $tmpStart->format("M") . " " . ($tmpEnd->diff($tmpStart)->format("%a")+1) . "\n";
$tmpStart = $tmpEnd->modify('first day of next month');
}
This outputs:
Jan 31
Feb 28
Mar 31
Apr 30
May 15
get the number of days in month from cal_days_in_month() function and use date_diff() to compare between those
If I understand correctly, you want to find the difference in days between two dates?
<?php
$startDate = mktime(0, 0, 0, 1, 1, 1970); //Enter your start date and time
$endDate = mktime(0, 0, 0, 5, 20, 2014); //Enter your end date and time
echo ($endDate-$startDate)/60/60/24; //This works out the number of days between the two
?>
Outputs:
16210
Look at the mktime() documentation to understand how to input your date into the mktime() function.
Related
This question already has answers here:
Get the last 12 months in PHP
(5 answers)
Closed 3 years ago.
I'm trying to use a loop to show the last 12 months, but March appears twice.
for ($i=0; $i < 12; $i++) {
$month = date("d/m/Y", strtotime("now -$i month"));
echo "$month<br>";
}
Output:
30/01/2020
30/12/2019
30/11/2019
30/10/2019
30/09/2019
30/08/2019
30/07/2019
30/06/2019
30/05/2019
30/04/2019
30/03/2019
02/03/2019
How can I solve this?
Use the first day of the month as the basis in your script.
"first day of this month -$i month"
Use DateTime and keep track of month/year combinations that you already had:
$dt = new DateTime();
$previous = [];
for ($i=0; $i < 12; $i++) {
$month = $dt->format("d/m/Y");
echo "$month<br>".PHP_EOL;
$previous[$dt->format('Y-m')] = true;
$dt->modify('-1 month');
while (array_key_exists($dt->format('Y-m'), $previous)) {
$dt->modify('-1 day');
}
}
If this encounters a previously encountered month-year combination, it starts substracting days until it reaches the previous month.
Will produce this output:
30/01/2020
30/12/2019
30/11/2019
30/10/2019
30/09/2019
30/08/2019
30/07/2019
30/06/2019
30/05/2019
30/04/2019
30/03/2019
28/02/2019
You probably want to use DatePeriod for this task and not date. It's much simpler and more sane.
$start = new DateTime;
$start->setDate($start->format('Y'), $start->format('n'), 1); // Normalize the day to 1
$start->sub(new DateInterval('P12M'));
$interval = new DateInterval('P1M');
$recurrences = 12;
foreach (new DatePeriod($start, $interval, $recurrences, true) as $date) {
echo $date->format('F, Y'), "\n"; // attempting to make it more clear to read here
}
Output:
February, 2019
March, 2019
April, 2019
May, 2019
June, 2019
July, 2019
August, 2019
September, 2019
October, 2019
November, 2019
December, 2019
January, 2020
This question already has answers here:
Months with less than 31 days omitted from list
(3 answers)
Why does PHP date('m', strtotime('-1 months')) not work correctly for today? 07/31
(5 answers)
Month duplication occurs when DateTime called on end of a 31 day month
(1 answer)
Closed 5 years ago.
I want to output the last 13 months, beginning with last month.
my code:
for($i=1;$i<=(13);$i++)
{
echo date("m",strtotime("-".$i." month"));
}
It worksfine, but today (31.) it looks like this
07
07
05
05
03
03
01
12
12
10
10
08
07
I am missing the months, that do not have 31 days. How can I fix this?
Works with a DateTime object! ;-)
$date = new DateTime('2017-08-31');
for ($i=1; $i<=(13); $i++) {
$tmpDate = clone $date;
$tmpDate->modify('last day of -' . $i . ' month');
echo $tmpDate->format('Y-m-d') . "\n";
}
The trick is to copy the variable so that you can reduce the months times var $i. Using "last day of -x month" always picks the last day of that month.
Reference: https://secure.php.net/manual/en/class.datetime.php
This question already has answers here:
Months with less than 31 days omitted from list
(3 answers)
Closed 5 years ago.
I want to find 2 months after current month and previous month. I have coded as below,
$current = strtotime(date('m',time()) . '-01 00:00:01');
$next_month = date('m', strtotime('+1 month', $current));
$next_next_month = date('m', strtotime('+2 month', $current));
$previous_month = date('m', strtotime('-0 month', $current));
echo $previous_month.$next_month.$next_next_month;
output : 01 02 03
I want to get the result from current month. And the expected output is 04 05 06.
How to achieve this result. Is there any solution. Thankyou.
$m = date('m');
printf("%02d %02d %02d", ($m+10) % 12 + 1, $m, $m % 12 + 1);
Output (on 2nd April):
03 04 05
This question already has answers here:
Add number of days to a date
(20 answers)
Closed 6 years ago.
Hello all i am trying to add 30 days to my date. I am using below coding.
<?php
$next_due_date = date('05/06/2016', strtotime("+30 days"));
echo $next_due_date;
?>
But it is returning to "05/06/2016" only!
Please help me!
Do not use php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.
Use the DateTime class
<?php
$date = new DateTime('2016-06-06'); // Y-m-d
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
The reason you should avoid anything to do with UNIX timestamps (time(), date(), strtotime() etc) is that they will inevitably break in the year 2038 due to integer limitations.
The maximum value of an integer is 2147483647 which converts to Tuesday, 19 January 2038 03:14:07 so come this time; this minute; this second; everything breaks
Source
Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:
$now = strtotime('31 December 2019');
for ($i = 1; $i <= 6; $i++) {
echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}
You'd get the following sequence of dates:
31 December
31 November
31 October
31 September
31 August
31 July
31 June
PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:
01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19
Please try this.
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;
This will return 06/06/2016. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.
echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;
This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016
If the input date is going to be in mysql, you can perform this function on mysql directly, like this.
DATE_ADD(due_date, INTERVAL 1 MONTH);
The first parameter is the format, not the current date.
<?php
$next_due_date = date('d/m/Y', strtotime("+30 days"));
echo $next_due_date;
Demo: https://eval.in/583697
If you want to add the 30 days to a particular starting date use the second parameter of the strtotime function to tell it where to start.
When in doubt about how a function works refer to the manual.
http://php.net/manual/en/function.strtotime.phphttp://php.net/manual/en/function.date.php
You need to provide date format like d/m/Y instead of 05/06/2016
Try
$old_date = '05-06-2016';
$next_due_date = date('d-m-Y', strtotime($old_date. ' +30 days'));
echo $next_due_date;
$date = "1998-08-14";
$newdate = strtotime ( '30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
Found the above code
This question already has answers here:
Get Start and End Days for a Given Week in PHP
(16 answers)
Closed 9 years ago.
I am selecing a date from jquery datepicker and I want to pick the starting and ending date of the week that precedes this date. - for example picking April 10, 2013 should return March 31, 2013 and April 6, 2013.
Sunday is the first day of the week and Saturday is last.
Below is my code.
$weekday = $d->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 0); //to make week start from monday till sunday add 1 to weekday -
$start1 = $d->modify("-$diff day");
$start_date = $d->format('Y-m-d');
$end1 = $d->modify('+6 day');
$stop_date = $d->format('Y-m-d');
The code works fine, at least mostly. However, when I pick any sunday I get a wrong a non expected result which is the previous week of the previous week.
for example, if I select March 31, 2013 - I should get march 24 and march 30,, but I am getting march 28 and march 24.
Where am I going wrong ?
Try this, it should solve your problem:
$input = 'April 10, 2013'; // come from jquery
$dt = new DateTime($input . ' -1week');
$monday = clone $dt->modify(('Sunday' == $dt->format('l')) ? 'Monday last week' : 'Monday this week');
$sunday = clone $dt->modify('Sunday this week');
printf("You've selected a date in the week from %s to %s\n",
$monday->format('Y-m-d'),
$sunday->format('Y-m-d')
);