PHP find nearest of two dates - php

How would I get the nearest of two dates once I've defined them both as variables?
Here's the code to calculate the next Monday and Friday:
//calculates nearest Monday
$nextMonday = strtotime("next Monday");
$mondayParade = date("d/m/Y", $nextMonday);
//calculates nearest Friday
$nextFriday = strtotime("next Friday");
$fridayParade = date("d/m/Y", $nextFriday);
When echoing $mondayParade it displays 26/01/2015 which is correct. $fridayParade also works as above but shows friday's date.
I'd like to be able to calculate which of these two dates is closest to my current date.
I read about also using strtotime for this but can't figure out how.
Thanks

$now = time();
$nextMondayDiff = abs($now - $nextMonday);
$nextFridayDiff = abs($now - $nextFriday);
if ($nextMondayDiff < $nextFridayDiff) {
echo 'Monday is closer';
} else {
echo 'Friday is closer';
}
Or as #David points out in the comments, assuming both dates are always guaranteed to be in the future:
if ($nextMonday < $nextFriday) {
echo 'Monday is closer';
} else {
echo 'Friday is closer';
}

if($nextMonday < $nextFriday) {
echo "Monday";
} else {
echo "Friday";
}

Related

Closest day of week to specific date

I'm trying to calculate National Sovereignty Day of Argentina which is celebrated on the Monday closest to 20 November.
I tried to find PHP solutions here but just found a Ruby solution: Get the closest date of a specific week day
How can I implement this for PHP?
Using $date->format("N") you will get a numeric representation of the weekday (1-7 for Monday-Sunday). If its greater than 4, which is greater than Thursday, select the next Monday. If not, it's the previous Monday.
If $date->format("N") === 1, it's the date provided, and we don't need any modifications.
$date = new DateTime("November 20th");
$dayOfWeek = $date->format("N");
if ($dayOfWeek > 1) {
$date->modify($dayOfWeek > 4 ? 'next monday' : 'previous monday');
}
echo $date->format("Y-m-d");
Live demo
Could try something like this:
<?php
function getSovDay($year)
{
$date = new \DateTime($year."-11-20");
$weekDay = $date->format("N");
if ($weekDay == 1)
{
return $date;
} elseif ($weekDay <= 4) {
return $date->modify("previous monday");
} else {
return $date->modify("next monday");
}
}
for($i = 2000; $i <= 2200; $i++)
{
echo getSovDay($i)->format("Y-m-d")."\n";
}

Calendar week base php

I want to make a calendar based on weeks with php found this script. If i get week 18 the first day is in april. How can i make it wright &bnsp; for the first day?
If you look at Juni there is 4 blank days in the begining of the week then i need php to print 4 &bsp; (blank day) hope you can help.
$week_number = 18;
$year = 2018;
if($week_number < 10){
$week_number = "0".$week_number;
}
for($day=1; $day<=7; $day++)
{
echo date('d', strtotime($year."W".$week_number.$day))."\n";
}
This will do what you want.
I save the days in an array instead and then find the key of the lowest number (that's the start of next month).
Then I loop the array and if the key is less than the start position I echo new line, else the value.
for($day=1; $day<=7; $day++){
$days[$day] = date('d', strtotime($year."W".$week_number.$day))."\n";
}
$start = array_keys($days, min($days))[0];
for($day=1; $day<=7; $day++){
If($day<$start){
Echo "\n";
}Else{
Echo $days[$day];
}
}
https://3v4l.org/dIhYZ
Ok works good to hard for me to do myself now, but i will lern. That works for the start of the week but how it will look at the end of the week? The 31st is on a thursday.

php comparing dates for day change

I have found hundreds of questions and answers for topics SIMILAR to this on SO, however, none match my needs specifically and I am stumped.
I have a variable in y-m-d format and I need to see if it was created on the previous calendar day (not 24 hours previous, but on the previoous calendar day).
ie.
$tDate = '12-05-2';
If object created May 2, 2012 at 11:59pm(stored time) I need a comparison to May 3, 2012 12:01 am(current time) to equal true.
If object created May 2, 2012 at 11:51pm(stored time) I need a comparison to May 2, 2012 11:58pm(current time) to equal false.
I know if these were stored in a MySQL db and pulled from a field, MySQL could figure that out easily. In this case, however, that solution is not an option.
This comparison must be done entirely in php.
I know it's an eccentric question, but hey, that's what the guru's at StackOverflow excel at! Looking forward to seeing the replies!
UPDATE
Figured this out as:
$dTest = '12-05-02';
$dTest = explode('-',$dTest);
$dTest2 = date('y-m-d');
$dTest2 = explode('-',$dTest2);
if ($dTest[2]<$dTest2[2]){
echo '<br />Posted Yesterday<br />';
} else {
echo '<br />Posted Today<br />';
}
Is there a more efficient solution? Seems to work, but I figure there must be a more optimal/elegant solution?
SOLVED
$tHolder = '12-05-12';
$voteDate = date("y-m-d", strtotime($tHolder));
$today = date("y-m-d", strtotime("today"));
if ($voteDate === $today)
{
echo "this was today's post";
}
elseif ($voteDate < $today)
{
echo "this was previous to today";
}
Firstly - I dont think your "solutions" works. What happens when todays date is 12-06-01 and the post was on 12-05-31 - it will give the wrong answer because "31" > "1"
Anyway - I think the correct answer is:
$yesterday =date("y-m-d", strtotime("yesterday"));
$today = date("y-m-d", strtotime("today"));
if ($yesterday === $tDate)
{
echo "this was yesterdays post";
}
elseif ($today === $tDate)
{
echo "this was todays post";
}
else
{
echo "this was NOT yesterday or today":
}
You can convert both dates to UNIX time and then compare it as integers:
$day_start = strtotime('-1 day', mktime(0, 0, 0);
$day_finish = strtotime('-1 day', mktime(23, 59, 59);
$dT = strtotime('-1 day', $dTime)
if($dT > $day_start && $dT < $day_finish) {
var_dump($dT);
} else {
exit;
}
If you're actually looking for "Posted X days ago":
$datetime1 = new DateTime('2012-05-01');
$datetime2 = new DateTime('2012-05-02');
$interval = (int)$datetime1->diff($datetime2)->format('%a');
switch ($interval) {
case 0:
echo "Posted Today<br />";
break;
case 1:
echo "Posted $interval day ago<br />";
break;
default:
echo "Posted $interval days ago<br />";
}

PHP Accurately Calculate Nearest Age for a given DOB

I am trying to calculate the nearest Age based on DOB, but i cant wrap my head around how to do it. I have tried some methods which estimates but this is not good enough. We need to calculate the days from today and the next birthday, whether it is in the current year or next year. and also calculate the days from today and the last birthday again whether it is in the current year or last year.
Any suggestions?
I think this is what you want.... of course, you could just get a persons age accurate to the day and round it up or down to the closest year..... which is probably what I should have done.
It's quite brute force, so I'm sure you can do it better, but what it does is check the number of days until this year's, next year's, and last year's birthday (I checked each of those three separately instead of subtracting from 365, since date() takes care of leap years, and I don't want to). Then it calculates age from whichever one of those birthdays is closest.
Working example
<?php
$bday = "September 3, 1990";
// Output is 21 on 2011-08-27 for 1990-09-03
// Check the times until this, next, and last year's bdays
$time_until = strtotime(date('M j', strtotime($bday))) - time();
$this_year = abs($time_until);
$time_until = strtotime(date('M j', strtotime($bday)).' +1 year') - time();
$next_year = abs($time_until);
$time_until = strtotime(date('M j', strtotime($bday)).' -1 year') - time();
$last_year = abs($time_until);
$years = array($this_year, $next_year, $last_year);
// Calculate age based on closest bday
if (min($years) == $this_year) {
$age = date('Y', time()) - date('Y', strtotime($bday));
}
if (min($years) == $next_year) {
$age = date('Y', strtotime('+1 year')) - date('Y', strtotime($bday));
}
if (min($years) == $last_year) {
$age = date('Y', strtotime('-1 year')) - date('Y', strtotime($bday));
}
echo "You are $age years old.";
?>
Edit: Removed unnecessary date()s in the $time_until calcs.
If I understand correctly you want to "round" the age? Then how about something along these lines:
$dob = new DateTime($birthday);
$diff = $dob->diff(new DateTime);
if ($diff->format('%m') > 6) {
echo 'Age: ' . ($diff->format('%y') + 1);
} else {
echo 'Age: ' . $diff->format('%y');
}
Edit: rewrote to use DateInterval
This should do the trick for you...
$birthday = new DateTime('1990-09-03');
$today = new DateTime();
$diff = $birthday->diff($today, TRUE);
$age = $diff->format('%Y');
$next_birthday = $birthday->modify('+'. $age + 1 . ' years');
$halfway_to_bday = $next_birthday->sub(DateInterval::createFromDateString('182 days 12 hours'));
if($today >= $halfway_to_bday)
{
$age++;
}
echo $age;

Next business day of given date in PHP

Does anyone have a PHP snippet to calculate the next business day for a given date?
How does, for example, YYYY-MM-DD need to be converted to find out the next business day?
Example:
For 03.04.2011 (DD-MM-YYYY) the next business day is 04.04.2011.
For 08.04.2011 the next business day is 11.04.2011.
This is the variable containing the date I need to know the next business day for
$cubeTime['time'];
Variable contains: 2011-04-01
result of the snippet should be: 2011-04-04
Next Weekday
This finds the next weekday from a specific date (not including Saturday or Sunday):
echo date('Y-m-d', strtotime('2011-04-05 +1 Weekday'));
You could also do it with a date variable of course:
$myDate = '2011-04-05';
echo date('Y-m-d', strtotime($myDate . ' +1 Weekday'));
UPDATE: Or, if you have access to PHP's DateTime class (very likely):
$date = new DateTime('2018-01-27');
$date->modify('+7 weekday');
echo $date->format('Y-m-d');
Want to Skip Holidays?:
Although the original poster mentioned "I don't need to consider holidays", if you DO happen to want to ignore holidays, just remember - "Holidays" is just an array of whatever dates you don't want to include and differs by country, region, company, person...etc.
Simply put the above code into a function that excludes/loops past the dates you don't want included. Something like this:
$tmpDate = '2015-06-22';
$holidays = ['2015-07-04', '2015-10-31', '2015-12-25'];
$i = 1;
$nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday'));
while (in_array($nextBusinessDay, $holidays)) {
$i++;
$nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday'));
}
I'm sure the above code can be simplified or shortened if you want. I tried to write it in an easy-to-understand way.
For UK holidays you can use
https://www.gov.uk/bank-holidays#england-and-wales
The ICS format data is easy to parse. My suggestion is...
# $date must be in YYYY-MM-DD format
# You can pass in either an array of holidays in YYYYMMDD format
# OR a URL for a .ics file containing holidays
# this defaults to the UK government holiday data for England and Wales
function addBusinessDays($date,$numDays=1,$holidays='') {
if ($holidays==='') $holidays = 'https://www.gov.uk/bank-holidays/england-and-wales.ics';
if (!is_array($holidays)) {
$ch = curl_init($holidays);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$ics = curl_exec($ch);
curl_close($ch);
$ics = explode("\n",$ics);
$ics = preg_grep('/^DTSTART;/',$ics);
$holidays = preg_replace('/^DTSTART;VALUE=DATE:(\\d{4})(\\d{2})(\\d{2}).*/s','$1-$2-$3',$ics);
}
$addDay = 0;
while ($numDays--) {
while (true) {
$addDay++;
$newDate = date('Y-m-d', strtotime("$date +$addDay Days"));
$newDayOfWeek = date('w', strtotime($newDate));
if ( $newDayOfWeek>0 && $newDayOfWeek<6 && !in_array($newDate,$holidays)) break;
}
}
return $newDate;
}
function next_business_day($date) {
$add_day = 0;
do {
$add_day++;
$new_date = date('Y-m-d', strtotime("$date +$add_day Days"));
$new_day_of_week = date('w', strtotime($new_date));
} while($new_day_of_week == 6 || $new_day_of_week == 0);
return $new_date;
}
This function should ignore weekends (6 = Saturday and 0 = Sunday).
This function will calculate the business day in the future or past. Arguments are number of days, forward (1) or backwards(0), and a date. If no date is supplied todays date will be used:
// returned $date Y/m/d
function work_days_from_date($days, $forward, $date=NULL)
{
if(!$date)
{
$date = date('Y-m-d'); // if no date given, use todays date
}
while ($days != 0)
{
$forward == 1 ? $day = strtotime($date.' +1 day') : $day = strtotime($date.' -1 day');
$date = date('Y-m-d',$day);
if( date('N', strtotime($date)) <= 5) // if it's a weekday
{
$days--;
}
}
return $date;
}
What you need to do is:
Convert the provided date into a timestamp.
Use this along with the or w or N formatters for PHP's date command to tell you what day of the week it is.
If it isn't a "business day", you can then increment the timestamp by a day (86400 seconds) and check again until you hit a business day.
N.B.: For this is really work, you'd also need to exclude any bank or public holidays, etc.
I stumbled apon this thread when I was working on a Danish website where I needed to code a "Next day delivery" PHP script.
Here is what I came up with (This will display the name of the next working day in Danish, and the next working + 1 if current time is more than a given limit)
$day["Mon"] = "Mandag";
$day["Tue"] = "Tirsdag";
$day["Wed"] = "Onsdag";
$day["Thu"] = "Torsdag";
$day["Fri"] = "Fredag";
$day["Sat"] = "Lørdag";
$day["Sun"] = "Søndag";
date_default_timezone_set('Europe/Copenhagen');
$date = date('l');
$checkTime = '1400';
$date2 = date(strtotime($date.' +1 Weekday'));
if( date( 'Hi' ) >= $checkTime) {
$date2 = date(strtotime($date.' +2 Weekday'));
}
if (date('l') == 'Saturday'){
$date2 = date(strtotime($date.' +2 Weekday'));
}
if (date('l') == 'Sunday') {
$date2 = date(strtotime($date.' +2 Weekday'));
}
echo '<p>Næste levering: <span>'.$day[date("D", $date2)].'</span></p>';
As you can see in the sample code $checkTime is where I set the time limit which determines if the next day delivery will be +1 working day or +2 working days.
'1400' = 14:00 hours
I know that the if statements can be made more compressed, but I show my code for people to easily understand the way it works.
I hope someone out there can use this little snippet.
Here is the best way to get business days (Mon-Fri) in PHP.
function days()
{
$week=array();
$weekday=["Monday","Tuesday","Wednesday","Thursday","Friday"];
foreach ($weekday as $key => $value)
{
$sort=$value." this week";
$day=date('D', strtotime($sort));
$date=date('d', strtotime($sort));
$year=date('Y-m-d', strtotime($sort));
$weeks['day']= $day;
$weeks['date']= $date;
$weeks['year']= $year;
$week[]=$weeks;
}
return $week;
}
Hope this will help you guys.
Thanks,.
See the example below:
$startDate = new DateTime( '2013-04-01' ); //intialize start date
$endDate = new DateTime( '2013-04-30' ); //initialize end date
$holiday = array('2013-04-11','2013-04-25'); //this is assumed list of holiday
$interval = new DateInterval('P1D'); // set the interval as 1 day
$daterange = new DatePeriod($startDate, $interval ,$endDate);
foreach($daterange as $date){
if($date->format("N") <6 AND !in_array($date->format("Y-m-d"),$holiday))
$result[] = $date->format("Y-m-d");
}
echo "<pre>";print_r($result);
For more info: http://goo.gl/YOsfPX
You could do something like this.
/**
* #param string $date
* #param DateTimeZone|null|null $DateTimeZone
* #return \NavigableDate\NavigableDateInterface
*/
function getNextBusinessDay(string $date, ? DateTimeZone $DateTimeZone = null):\NavigableDate\NavigableDateInterface
{
$Date = \NavigableDate\NavigableDateFacade::create($date, $DateTimeZone);
$NextDay = $Date->nextDay();
while(true)
{
$nextDayIndexInTheWeek = (int) $NextDay->format('N');
// check if the day is between Monday and Friday. In DateTime class php, Monday is 1 and Friday is 5
if ($nextDayIndexInTheWeek >= 1 && $nextDayIndexInTheWeek <= 5)
{
break;
}
$NextDay = $NextDay->nextDay();
}
return $NextDay;
}
$date = '2017-02-24';
$NextBussinessDay = getNextBusinessDay($date);
var_dump($NextBussinessDay->format('Y-m-d'));
Output:
string(10) "2017-02-27"
\NavigableDate\NavigableDateFacade::create($date, $DateTimeZone), is provided by php library available at https://packagist.org/packages/ishworkh/navigable-date. You need to first include this library in your project with composer or direct download.
I used below methods in PHP, strtotime() does not work specially in leap year February month.
public static function nextWorkingDay($date, $addDays = 1)
{
if (strlen(trim($date)) <= 10) {
$date = trim($date)." 09:00:00";
}
$date = new DateTime($date);
//Add days
$date->add(new DateInterval('P'.$addDays.'D'));
while ($date->format('N') >= 5)
{
$date->add(new DateInterval('P1D'));
}
return $date->format('Y-m-d H:i:s');
}
This solution for 5 working days (you can change if you required for 6 or 4 days working). if you want to exclude more days like holidays then just check another condition in while loop.
//
while ($date->format('N') >= 5 && !in_array($date->format('Y-m-d'), self::holidayArray()))

Categories