How many days until X-Y-Z date?
<?php
$cdate = mktime(0, 0, 0, 12, 31, 2009);
$today = time();
$difference = $cdate - $today;
if ($difference < 0) { $difference = 0; }
echo floor($difference/60/60/24)." days remaining";
?>
How many days left till any date?
As per the PHP documentation:
<?php
$year = '2012';
$month = '12';
$day = '26';
$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
$end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
$interval = $current_date->diff($end_date);
echo $interval->format('%a day(s)');
?>
Translate date value into how many days
You can simply do it by getting date
difference.
Here, selected_date
is your date.
Ideally, you should only use date
part for getting your result.
var cur_date = "2020-01-08 12:23:12"var selected_date = "2020-01-01 10:23:33"var cur_date_date_part = new Date(cur_date).getFullYear()+ '-'+(new Date(cur_date).getMonth()+1)+'-'+new Date(cur_date).getDate()
var selected_date_date_part = new Date(selected_date).getFullYear()+ '-'+(new Date(selected_date).getMonth()+1)+'-'+new Date(selected_date).getDate()
var resultDays = ((Date.parse(cur_date_date_part) - Date.parse(selected_date_date_part)) / 86400000)if (resultDays > 0){ console.log(Math.abs(resultDays) + " days ago") }else{ console.log(Math.abs(resultDays) + " days to go") }
count how many days within a date range are within another date range
The key to this problem is to simplify it as much as possible. I think using an array as a lookup table for the cost of each day of the year is the way to go. The first thing to do then, is to generate the array. The array just represents each day of the year and doesn't represent any particular year. I chose to use 2012 to generate the lookup array as it is a leap year and so has every possible day in it.
function getSeasonArray()
{
/**
* I have chosen 2012 as it was a leap year. All we want to do is
* generate an array which has avery day of the year in it.
*/
$startDate = new DateTime('1st January 2012');
//DatePeriod always drops the last day.
$endDate = new DateTime('1st January 2013');
$season2Start = new DateTime('1st April 2012');
$season2End = new DateTime('1st October 2012');
$allDays = new DatePeriod($startDate, new DateInterval('P1D'), $endDate);
$season2Days = new DatePeriod($season2Start, new DateInterval('P1D'), $season2End);
$seasonArray = array();
foreach($allDays as $day){
$seasonArray[] = $day->format('d-M');
$seasonArray[$day->format('d-M')]['season'] = 1;
}
foreach($season2Days as $day){
$seasonArray[$day->format('d-M')]['season'] = 2;
}
return $seasonArray;
}
Once that is done you just need the period over which to calculate:-
$bookingStartDate = new DateTime();//Or wherever you get this from
$bookingEndDate = new DateTime();
$bookingEndDate->setTimestamp(strtotime('+ 7 month'));//Or wherever you get this from
$bookingPeriod = new DatePeriod($bookingStartDate, new DateInterval('P1D'), $bookingEndDate);
Then we can do the calculation:-
$seasons = getSeasonArray();
$totalCost = 0;
foreach($bookingPeriod as $day){
$totalCost += $seasons[$day->format('d-M')]['season'];
var_dump($day->format('d-M') . ' = $' . $seasons[$day->format('d-M')]['season']);
}
var_dump($totalCost);
I have chosen a long booking period, so that you can scan through the var_dump() output and verify the correct price for each day of the year.
This is a quick stab done between distractions at work and I'm sure that with a bit of thought you can mould it into a more elegant solution. I'd like to get rid of the double iteration for example, unfortunately, work pressures prevent me from spending further time on this.
See the PHP DateTime man page for further information on these useful classes.
Trying to calculate days to next birthday
select
date_field,
abs(if(right(curdate(),5) >= right(date_field,5),
datediff(curdate(),concat(year(curdate()+ interval 1 year),right(date_field,6))) ,
datediff(concat(year(curdate()),right(date_field,6)),curdate()))) as days
from table
Related Topics
Reusing the Same Curl Handle. Big Performance Increase
Return Errors from PHP Run Via. Ajax
Codeigniter Model Error: Undefined Property
MySQL Join Multiple Rows as Columns
PHP Curl How to Add the User Agent Value or Overcome the Servers Blocking Curl Requests
Forcing Access to _Php_Incomplete_Class Object Properties
How to Trust PHP _Destruct() Method to Be Called
Detecting File Upload Size on the Client Side
Getting a List of Files by Folder on Drive Sdk
Tablegateway with Multiple from Tables
Get Start and End Days for a Given Week in PHP
Understanding PHP Curl_Multi_Exec
Soapfault Exception: Could Not Connect to Host
How to Stop PHP Imagick Auto-Rotating Images Based on Exif 'Orientation' Data
How Is an Array in a PHP Foreach Loop Read
Laravel 5.3 Withcount() Nested Relation
Make Text Wrap in a Cell with Fpdf
How to Send a Bytearray (From Flash) and Some Form Data to PHP