Days between two dates in Javascript or Jquery - php

I want some calculation with Javascript or jquery. actually I have two inputs with date picker. I want when I selected dates in both field a new field value should appear which will have the total number of days for selected date. For example:
I select in input one : 2012-01-01
and
I select in input two: 2013-01-01
then in field three value should be = 365 Days
Hope you understand.
I know how to count days between two dates:
$days = (strtotime($termi_date) - strtotime($str_date)) / (60 * 60 * 24);
echo $days;

var from_date = $("#from_input").datepicker('getDate'),
to_date = $("#to_input").datepicker('getDate');
var diff_in_milliseconds = to_date.getTime() - from_date.getTime();
or simply
diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day
DEMO

Related

Calculate future date using interval (in number of weeks) and a starting date

I'm trying to write a function to calculate the next date that a piece of equipment needs to be checked. I'm using the code below (it's incomplete.)
function get_next_check(){
$today = date(get_option('date_format'));
$first_check = types_get_field_meta_value( 'first_check', $post_id );
// Interval is a number of weeks- ie. month = 4, year = 52
$interval = types_get_field_meta_value( 'interval', $post_id );
// Calculate the next date after today that the check needs to be performed
$next_check = ;
return $next_check;
}
add_shortcode( 'next_check', 'get_next_check' );
I'm guessing I need to create an array of all possible dates, and compare each to today's date, only returning the next one?
Assuming that $first_check is a string with the date in it, and you want the function to return the same, something like this might work:
$week = 60 * 60 * 24 * 7; // number of seconds in a week
$next_check = strtotime($first_check);
while ($next_check < time()) {
$next_check += $week * $interval; // advance the check time by the desired interval
}
return date(get_option('date_format'), $next_check);
You can eliminate your initialization of the $today variable with this.
You can achieve this on the proposed by Greg Schmidt , or alternatively by doing:
// Calculate the next date after today that the check needs to be performed
$next_check = date(get_option('date_format'), strtotime($today." ".$interval." weeks"));

Counting number of days between 2 dates when exceed in 1 hour add another 1 day

I'm having problem when getting the exact number of days. Given I have date/time which consider hours in counting number of days below the code give me zero days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-07 11:10:00");
$days=$fisrstDate->diff($secondDate)->days;
another example is this it should give me 2 days but shows only 1 days my idea is when 24 hours exceed I want to add another 1 days so that it would give me an output of 2 days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-08 05:00:00");
$days=$fisrstDate->diff($secondDate)->days;
You can use strtotime to get the exact seconds between two time stamps and then convert it to days followed by ceil to make it work. Eg:
$fisrstDate = strtotime("2018-03-07 04:46:00");
$secondDate = strtotime("2018-03-07 11:10:00");
$days = abs(ceil((abs($fisrstDate - $secondDate)/ (60 * 60 * 24)) - (1 / 24)));
echo $days;
Isn't it just date2 - date1 + 1?

Working datetime between php and mysql

I'm just want to check if my way is correct or not. I want to calculate the remaining time for an event.
There are 3 component for it.
The start datetime (in mysql datetime), which is retrieved from mysql.
The duration (minutes in integer), which is also retrieved from mysql.
The current datetime, which is retrieved from php function now().
To count:
The remaining time (in time hh:mm:ss), which is from formula (start + duration) - now
My propose code is:
$row = data->fetch_assoc();
$start = $row["start_time"]; // e.g: "2015-06-19 09:37:16"
$duration = $row["duration"]; // e.g: 60 (minutes)
$now = time(); // e.g: 1434648994
$start_dt = strtotime ($start);
$remaining = ($start_dt + $duration * 60) - $now;
echo "Remaining time: ".$remaining. " seconds."
Is this correct?
Your code now calculates time until the end of the event.
And if $start_dt is lower then $now you get negative value.

Count days within a month from date range

I have three date ranges in mysql table as follow
from 2013-09-29 to 2013-10-02
from 2013-10-14 to 2013-10-16
from 2013-10-28 to 2013-11-05
I want to count only days that occur in Month of October, for example from first range (2013-09-29 to 2013-10-02) I should get difference of two days (1st and 2nd October) , and it should ignore days from September month, Finally i want to count total days in a given month from above date ranges.
Can it be done from direct mysql query. or any short PHP logic.
here is my table structure
id,employee_id,leave_start,leave_to
I am looking for a method some thing like
function countLeaves($employee_id,$month)
{
// code
return $numberOfLeaves
}
You need to calculate the first day of the month 2013-10-01 and the last day of the month 2013-10-31 and then you could use a query like this:
SELECT
DATEDIFF(
LEAST(d_end, '2013-10-31'),
GREATEST(d_start, '2013-10-01'))+1 days
FROM
ranges
WHERE
d_start<='2013-10-31' AND d_end>='2013-10-01'
Please see fiddle here.
function getAllDates($fromDate, $toDate,$rejectmonth)
{
if(!$fromDate || !$toDate ) {return false;}
$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24))
{
if($rejectmonth == date("m",$currentDateTS))
continue;
$currentDateStr = date("Y-m-d",$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
}
return $dateMonthYearArr;
}
use this function will return the array dates which are in the date ranges.
getAllDates('2013-09-10','2013-19-10',10);
$month = strtotime(date('Y-m-01', time()));
$daysCount = (int)(time() - $month) / (24 * 3600);
and if you need to check from any date to any date:
/**
* #var $dateFrom string Date to count from,
* you can use date('Y-m-01') as start of current month
* #var $dateTo string End of period date, example: '2013-09-05'
**/
function getDaysCount($dateFrom, $dateTo)
{
return (int)(strtotime($dateTo) - strtotime($dateFrom)) / (24 * 3600);
}

Date Calculation

i have a form with a fix date and time entry. (eg: 23:30 - 04/14/2011)
i have a second time entry. (eg: 00:15 - the Next Day)
how can i calculate the Date of the second entry time.
with only on these 3 variables.
Time A = 23:30 on the 04/14/2011
Time B = 00:15 on the ____ ?
Can anyone show me the semantics of this calculation how to find the Date of Time B!
Thanks
I can see a couple of ways of doing this. I would probably do something like this:
In HTML (missing form controls, labels etc):
<input name="date_from" type="text"> <!-- use some sort of javascript date popup control to format this properly-->
<input type="text" name="hours_to"> <!-- remember to add validation to input -->
<input type="text" name="mins_to"> <!-- remember to add validation to input -->
<select name="day_to">
<option value="0">The same day</option>
<option value="1">The next day</option>
<option value="2">The day after that</option>
</select>
Then in your PHP form handler, assuming date_from is in a valid date format:
$time_from = strtotime($_POST['date_from']);
$time_to = str_to_time('Y-m-d 00:00:00', $time_from) //start of the day
+ 86400 * (int) $_POST['day_to'] //86400 seconds in a day
+ 3600 * (int) $_POST['hour_to'] //3600 seconds in an hour
+ 60 * (int) $_POST['min_to']; //60 seconds in a minute
$sql_from = date("Y-m-d H:i:s", $time_from);
$sql_to = date("Y-m-d H:i:s", $time_to);
Then you can do your db insert, subject to validations etc of course.
Also, strtotime() can accept relative time parameters (e.g. '+ 1 day') which might be another way of doing this, check the PHP manual for more info
var date1 = '23:30 - 04/14/2011';
var date2 = '00:15 - the Next Day';
var d1month = date1.split(' - ')[1].split('/')[0];
var d1day = date1.split(' - ')[1].split('/')[1];
var d1year = date1.split(' - ')[1].split('/')[2];
var d1hour = date1.split(' - ')[0].split(':')[0];
var d1min = date1.split(' - ')[0].split(':')[1];
var d2hour = date2.split(' - ')[0].split(':')[0];
var d2min = date2.split(' - ')[0].split(':')[1];
var d = new Date(d1year, parseInt(d1month)-1, d1day, d1hour, d1min, 0, 0);
var d2 = new Date(d1year, parseInt(d1month)-1, parseInt(d1day)+1, d2hour, d2min, 0, 0);

Categories