get Number of month from a date php - php

Suppose I have a date specific as 2016-11-14 .
Also I have another date (starting) 02-14-2017 and ending 02-30-2017
I want to count the months that has passed is the date 2016-11-14 until the set of Range date.
I tried searching but I cant find a link on how to find months that has passed from a date to set of range date

DateTime() makes doing date math easy:
$datetime1 = new DateTime('2016-12-01');
$datetime2 = new DateTime('2017-02-01');
$interval = $datetime2->diff($datetime1);
echo (($interval->format('%y') * 12) + $interval->format('%m'));
Demo

Related

How to calculate months between two dates including the starting date month?

I'm trying to apply the straight line deprecation for this, I've two date starting date 2018-09-17 (YYYY-MM-DD) ending date 2018-12-30. I need answer in months which is 4 not 3 because, it give me 3.
I've tried starting date 2018-09-01 and ending date 2019-01-01. It give me correct answer 4 what I want.
It give me answer 4
$d1 = new DateTime("2018-09-01");
$d2 = new DateTime("2019-01-01");
echo $d1->diff($d2)->m . " Months";
I need answer 4 for these two date
2018-17-09 to 2018-30-12
Is there any way to get the answer 4 in months including the starting date. I am getting the answer 4 from these date (2018-09-01) to (2019-01-01). In short, I want to include the current date month.
You can use Carbon:
use \Carbon\Carbon;
$from = Carbon::createFromFormat('Y-d-m', '2018-17-09');
$to = Carbon::createFromFormat('Y-d-m', '2018-30-12')->addMonth();
return $to->diffInMonths($from);
Live demo here
You can easily use format method and pass "n" to it this will get the month as a numeric value then you can define your logic.
An example:
$date1 = (new Datetime("2018-09-17"))->format("n");
$date2 = (new Datetime("2018-12-30"))->format("n");
echo $date2 - $date1 + 1;

Form Field type is Date DateDiff

I have a form with Start & End date fields. The user plugs in the dates and runs a report.
I'm trying to find the total number of days between these dates.
Currently the form fields are setup type=date. The date format is 01-01-2017.
I have tried a lot but this seems to have me pretty close.
$end_date = date("m-d-y", strtotime($endd));
$start_date = date("m-d-y", strtotime($startd));
$reportdays = ($syour_date - $eyour_date);
Any help would be appreciated.
The date function is used to format a date for printing. It cannot be used to subtract two dates. You are basically trying to subtract two date strings.
'2016-12-12' - '2017-01-12' == 'invalid!'
You should use the DateTime:::diff() function to get the difference in days like this:
$end_date = new Date($endd);
$start_date = new Date($startd);
$interval = $end_date->diff($start_date);
$reportdays = $interval->format('%d');
You can read more about this here:
http://php.net/manual/en/datetime.diff.php

Next delivery date listed on website

I'm building a website for a business that makes deliveries every second Thursday. I need to display when the next delivery is going to be, and have that date change to two weeks forward when the previous delivery date is reached.
Based on what I've been able to research so far, I've cobbled together this code:
$start_date = '2016-10-27'; // next delivery date to start counting from
// create a DateTime object that represents start of sequence
$start_datetime = DateTime::createFromFormat('Y-m-d', $start_date);
// create a DateTime object representing the current date
$current_datetime = new DateTime('today');
$date_interval = new DateInterval('P2W'); // for delivery every 2 weeks
// determine end date for DatePeriod object that will later be used
// this is no further out than current date plus the interval
$end_datetime = new DateTime('tomorrow');
$end_datetime->add($date_interval);
$date_period = new DatePeriod($start_datetime, $date_interval, $end_datetime);
// iterate until the last date in the set
foreach($date_period as $dp) {
$next_delivery = $dp;
}
?>
<div class="header-next-delivery">
Next delivery: <?php echo $next_delivery->format('l, M j, Y'); ?>
</div>
This seems to work, but I can't help thinking that there must be a more elegant way to do this than having to iterate through a set of dates from the start date to the last date in the set. As time passes, the set will just get bigger and bigger.
Also, I'm having trouble figuring out the internal workings of these functions -- how would I set the exact time that the displayed delivery date bumps forward by two weeks?
Thanks for any insight!
You can get date interval in days from $start_date to current date. Divide it by 14 (two weeks), get remainder and substract it from 14. Then you can add that value of days to current date.
$start_date = date_create('2016-10-27');
$current_date = date_create();
$interval = date_diff($start_date, $current_date);
$days_diff = (int)$interval->format('%a');
$current_date->add(14 - ($days_diff % 14).' days');

How to determine if a date is more than three months past current date

I am getting a date back from a mysql query in the format YYYY-MM-DD.
I need to determine if that is more than three months in the past from the current month.
I currently have this code:
$passwordResetDate = $row['passwordReset'];
$today = date('Y-m-d');
$splitCurrentDate = explode('-',$today);
$currentMonth = $splitCurrentDate[1];
$splitResetDate = explode('-', $passwordResetDate);
$resetMonth = $splitResetDate[1];
$diferenceInMonths = $splitCurrentDate[1] - $splitResetDate[1];
if ($diferenceInMonths > 3) {
$log->lwrite('Need to reset password');
}
The problem with this is that, if the current month is in January, for instance, giving a month value of 01, and $resetMonth is November, giving a month value of 11, then $differenceInMonths will be -10, which won't pass the if() statement.
How do I fix this to allow for months in the previous year(s)?
Or is there a better way to do this entire routine?
Use strtotime(), like so:
$today = time(); //todays date
$twoMonthsLater = strtotime("+3 months", $today); //3 months later
Now, you can easily compare them and determine.
I’d use PHP’s built-in DateTime and DateInterval classes for this.
<?php
// create a DateTime representation of your start date
// where $date is date in database
$resetDate = new DateTime($date);
// create a DateIntveral representation of 3 months
$passwordExpiry = new DateInterval('3M');
// add DateInterval to DateTime
$resetDate->add($passwordExpiry);
// compare $resetDate to today’s date
$difference = $resetDate->diff(new DateTime());
if ($difference->m > 3) {
// date is more than three months apart
}
I would do the date comparison in your SQL expression.
Otherwise, PHP has a host of functions that allow easy manipulation of date strings:
PHP: Date/Time Functions - Manual

php find week number from specific date

I want to find a specific week number from the specific start date. For example $date is dragged from the database (i.e. 07/08/2011)
I want this to be the start date so it would be week 3 now from this date. This is the code i have so far but just shows the ISO version:
$date = strtotime("".$row['start_date']."");
$weekNumber = date("W", $date);
print $weekNumber;
I have googled for past two hours but cannot seem to find any thing that resolves this! any help would be great thanks!
Get the difference between now and the startdate, and then divide by seven days (7*86400 seconds)
<?php
$startdate = strtotime("".$row['start_date']."");
$enddate = time();
$time_passed = $enddate - $startdate;
// if the first day after startdate is in "Week 1" according to your count
$weekcount_1 = ceil ( $time_passed / (86400*7));
// if the first day after startdate is in "Week 0" according to your count
$weekcount_0 = floor ( $time_passed / (86400*7));
?>
You can drag week number from db directly along with main date.
For example,
"SELECT start_date, (WEEK(NOW()) - WEEK(start_date)) as desired_week as week from table";

Categories