I'm having trouble getting "weeks" in DateTime::diff function
Here's my code:
$date1 = new DateTime("2017-05-14");
$date2 = new DateTime("2017-06-14");
$interval = $date1->diff($date2);
echo $interval->m.' '.($interval->m > 1 ? 'months' : 'month');
It worked if I'm going to get the "month" count, but I want to get the weeks before turning it into a month:
We have 4 weeks in a month (4.34524 to be exact from Google Unit Converter), if the difference between start date and the date today exceeds 4 weeks, it should output "1 month" and so on..
Code (Demo):
$date1 = new DateTime("2017-06-1");
$date2 = new DateTime("2017-06-15");
$interval = $date1->diff($date2);
//var_export($interval);
if($interval->m>0){ // check if >= 1 month
echo "{$interval->m} month",($interval->m>1?'s':'');
}else{
$weeks=floor($interval->days/7); // if not yet 1 month, calc weeks
echo "$weeks week",$weeks!=1?'s':'';
}
// output: 2 weeks
Calculate days and then divide by 7 for week.
Try this code :
$date1 = new DateTime("2017-05-14");
$date2 = new DateTime("2017-06-14");
$interval = $date1->diff($date2);
$week = floor($date1->diff($date2)->days/7);
echo $week;
if($week > 4)
{
echo $interval->m.' '.($interval->m > 1 ? 'months' : 'month');
}
you could do this to get weeks and then make the condition:
$daysInAweek = 7;
$weeks = ($interval->days)/$daysInAweek;
if($weeks >= 4) {
echo 'is a month';
}
because a week have 7 days.
Related
I am trying to count the number of leap days within a date range.
This is what I have so far (adopted code from here)
<?php
$date_from = strtotime('2019-06-01');
$date_to = strtotime('2021-05-30');
$leapday_count = 0;
for($year=$date_from; $year<=$date_to; $year=strtotime('next year', $year)) {
if (date('L', $year)) {
$leapday_count++;
}
}
echo $leapday_count;
?>
Here is what I need help with:
The above code does only look at the years of the start and end date. For example it does not take into consideration if the start date is after Feb 29th. How can I make sure that it really takes the entire dates into consideration and not only the years of the dates?
Example expected result: 0 leap days between 2020-03-01 2024-02-28
Example expected result: 2 leap days between 2020-02-28 2024-03-01
Thanks in advance!
Stop doing date maths. It's hard.
Use datetime and diff
http://php.net/manual/en/datetime.diff.php
Example there
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
to exactly answer the question.
$start = new DateTime('2020-03-01');
$end = new DateTime('2024-02-28');
$interval = ("last day of feb next year");
$current = clone $start;
$leapYears = 0;
while ($current->modify("last day of feb") && $current <= $end) {
if ($current->format('d') == 29 && $current > $start) {
$leapYears++;
}
$current->modify("+1 year");
}
I am creating PHP function that will return difference between two dates in a format: 2 Months, 3 Weeks, 6 Days, 3 Hours. I have tried to use PHP DateTime class, but it returns only Months, Days and Hours and I can not find a way to calculate Weeks.
This is my function:
public function DateTimeDifference($FromDate, $ToDate) {
$FromDate = new DateTime($FromDate);
$ToDate = new DateTime($ToDate);
$Interval = $FromDate->diff($ToDate);
$Difference["Hours"] = $Interval->h;
$Difference["Days"] = $Interval->d;
$Difference["Months"] = $Interval->m;
return $Difference;
}
Now, I need $Difference["Weeks"] also included in return data.
EDIT: I know I can divide Days with 7 and get weeks, but this does not result right. For example: 2 Months, 14 Days, 3 Hours - When I divide 14 days with 7 I will get this: 2 Months, 2 Weeks, 14 Days, 3 Hours and now this is not same period.
public function DateTimeDifference($FromDate, $ToDate) {
$FromDate = new DateTime($FromDate);
$ToDate = new DateTime($ToDate);
$Interval = $FromDate->diff($ToDate);
$Difference["Hours"] = $Interval->h;
$Difference["Weeks"] = floor($Interval->d/7);
$Difference["Days"] = $Interval->d % 7;
$Difference["Months"] = $Interval->m;
return $Difference;
}
// this will only work from previous dates
// difference between utc date time and custom date time
function FromUtcToCustomDateTimeDifference($ToDate)
{
// Takes Two date time
$UTC_DATE = new DateTime('now', new DateTimeZone('UTC'));
$UTC_DATETIME = $UTC_DATE->format('Y-m-d H:i:s');
// add your own date time 1
$datetime1 = date_create(UTC_DATETIME);
$datetime2 = date_create($ToDate);
$Interval = date_diff($datetime1, $datetime2);
// Count Number Of Days Difference
$Day = $Interval->format('%a');
if($Day > 1)
{
$Month = $Interval->format('%m');
$Year = $Interval->format('%y');
$Week = (int)($Interval->format('%a')/7);
if($Year<1)
{
if($Day <= 7)
{
return $Day > 1 ? $Day.= " days ago" : $Day .= " day ago";
}
else if($Month<1)
{
return $Week > 1 ? $Week.= " weeks ago" : $Week .= " week ago";
}
return $Month > 1 ? $Month.= " months ago" : $Month .= " month ago";
}
else
{
return $Year > 1 ? $Year.= " years ago" : $Year .= " year ago";
}
}
else
{
return "today";
}
}
I like to iterate through all weeks in a date range that spawns 2 years. Starting at the current week number two years ago, until the current week number this year.
The problem is that a year can either have 52 weeks or 53 weeks, for example:
2015 had 53 weeks (the 53th was from 2015-12-28 till 2016-01-03)
2016 had 52 weeks (the 52th was from 2016-12-26 till 2017-01-01)
So this is currently my php code:
# start with the current week 2 years ago
$year = date("Y") - 2;
$week = (int) date("W"); // (int) removes leading zero for weeks < 10
$endYear = date("Y");
$endWeek = (int) date("W");
# iterate through all weeks until now
do {
echo $week. " of ". $year;
$week++;
if ($week > 52) { // or greater 53 ?????????????
$year ++;
$week = 1;
}
}
while ($year < $endYear || $week < $endWeek);
Instead of trying to keep track of the bounds, let PHP do it for you.
$start = new DateTime('-2 years');
$end = new DateTime();
$interval = new DateInterval('P1W');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('W') . " of ". $date->format('Y') . "\n";
}
Demo
With the help of Ross Wilson and John Conde I found a solution:
$now = new DateTime;
$from = (new DateTime)->setISODate($now->format('Y') - 2, $now->format('W'));
$from->modify("thursday this week"); // see ISO 8601
while($from < $now) {
echo $from->format('W Y').'<br />';
$from->modify("+1 week");
}
It is important so set the week day to thursday, because according to ISO 8601, the week "belongs" to the year which still contains the thursday.
You could use DateTime and setISODate for something like this:
$now = new DateTime;
$from = (new DateTime)->setISODate($now->format('Y') - 2, $now->format('W'));
while($from < $now) {
echo $from->format('W Y').'<br />';
$from->modify("1 week");
}
Hope this helps!
How can I return only 1 day before 1 week from today with dateTime?
I did this:
if ($_POST['interval'] == 88) {
$dayNumber = "7";
$pid = 'P1W';
}
$now = new DateTime($dayNumber . " days ago");
$interval = new DateInterval($pid); // 1 Day interval
$period = new DatePeriod($now, $interval, $dayNumber);
This returning day before 1 week and today but + all days between which is not good.
looking for something like this, Let me know if it works for you
$dateString = "2016-07-15"; // for example
$minus6 = date('Y-m-d', strtotime("$dateString -7 days"));
Well, your code is doind that: $now = new DateTime('7 days ago');
$now will returns '2016-07-08' from today (2016-07-15).
You dont need Interval and Period to return only one date.
I tried to find out difference between today date and specific day with format Ymd.
How to check whether specific day is greater than 30 days from today?
For example:
$date1 = '20160315'; // 2016-03-15
$date2 = '20160115'; // 2016-01-15
Try this
$date1=date_create('20160315');
$date2=date_create('20160115');
$diff=date_diff($date1,$date2);
$days = $diff->format("%a");
if($days > 30) do something
So simple...
$date1 = '20160315'; // 2016-03-15
$date2 = date(Ymd); // 2016-01-15
$day_difference = $date1 - $date2
if($day_difference > 30) {
echo 'specific day is greater than 30 days from today';
} else {
echo 'specific day is less than 30 days from today';
}
Try this:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-11-13');
$interval = $datetime1->diff($datetime2);
$int = $interval->format('%R%a');
if($int > +30) {
echo "Greater than 30 days";
} else {
echo "Less than 30 days";
}