Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

Friday 18 May 2018

Difference between two dates in PHP

<?php
$date1 = "2015-01-23 00:20";
$date2 = "2015-01-23 05:30";
$seconds = strtotime($date2) - strtotime($date1);
$days    = floor($seconds / 86400);
$hours   = floor(($seconds - ($days * 86400)) / 3600);
$minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60);
$seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));
echo "$days Days, $hours Hrs $minutes mins $seconds sec";
?>

Difference between two dates in Javascript with demo


Enter values in mm/dd/yy hh:mm:ss format
First date and time
Second date and time
Result
In (decimal) hours
In (decimal) minutes
In seconds

Code:
<script type='text/javascript'>
function p (i){
return Math.floor(i / 10) + "" + i % 10;
}
function trunc (i){
var j = Math.round(i * 100);
return Math.floor(j / 100) + (j % 100 > 0 ? "." + p(j % 100) : "");
}
function calculate (form){
var date1 = new Date(form.date1.value);
alert(form.date1.value+"\n\n"+date1)
var date2 = new Date(form.date2.value);
var sec = date2.getTime() - date1.getTime();
if (isNaN(sec)){
alert("Input data is incorrect!");
return;
}
if (sec < 0){
alert("The second date ocurred earlier than the first one!");
return;
}
var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;
form.result_h.value = trunc(sec / hour);
form.result_m.value = trunc(sec / minute);
form.result_s.value = trunc(sec / second);
var days = Math.floor(sec / day);
sec -= days * day;
var hours = Math.floor(sec / hour);
sec -= hours * hour;
var minutes = Math.floor(sec / minute);
sec -= minutes * minute;
var seconds = Math.floor(sec / second);
form.result.value = days + " day" + (days != 1 ? "s" : "") + ", " + hours + " hour" + (hours != 1 ? "s" : "") + ", " + minutes + " minute" + (minutes != 1 ? "s" : "") + ", " + seconds + " second" + (seconds != 1 ? "s" : "");
}
</script>
<form id="form">
<table cellpadding="3">
<tr>
<td colspan="2" align="center">Enter values in <font color="#800000">mm/dd/yy hh:mm:ss</font> format</td>
</tr>
<tr>
<td>First date and time</td>
<td><input type="text" name="date1" /></td>
</tr>
<tr>
<td>Second date and time</td>
<td><input type="text" name="date2" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" name="submit" value="Calculate" onclick="calculate(this.form)" /></td>
</tr>
<tr>
<td>Result</td>
<td colspan="2"><input type="text" name="result" readonly="readonly" size="40" /></td>
</tr>
<tr>
<td>In (decimal) hours</td>
<td colspan="2"><input type="text" name="result_h" readonly="readonly" /></td>
</tr>
<tr>
<td>In (decimal) minutes</td>
<td colspan="2"><input type="text" name="result_m" readonly="readonly" /></td>
</tr>
<tr>
<td>In seconds</td>
<td colspan="2"><input type="text" name="result_s" readonly="readonly" /></td>
</tr>
</table>

Calculate Number of days Between two dates except saturday & sunday or week off in PHP script


<?php
$strt_date = date_create('2015-m-1');
$end_date = date_create(date('Y-m-d'));
date_sub($strt_date, date_interval_create_from_date_string('1 day'));
$interval = date_diff($strt_date, $end_date);
$num=$interval->format('%a');
$x=1;
for($i=0;$i<$num;$i++){
  date_add($strt_date, date_interval_create_from_date_string('1 day'));
  $next_day=date_format($strt_date, 'd-m-Y');
  echo "<br>";
  $new_date = new DateTime($next_day);
  $weeknum=$new_date->format('w');
  if(($weeknum!=0)&&($weeknum!=6)){
	 echo "$x. not a holiday :$weeknum  $next_day ";
	 $x++;
  }
}
?>

Monday 18 August 2014

How to take dates between two dates in php?

   $task_to_do = array();
   echo "
"; $s = strtotime("START_DATE"); $d = strtotime("END_DATE"); $dif = 86400; $i=$s; $x=0; while ($i <= $d) { $dat = date("Y-m-d 00:00:00",$i); $t_s = date("Y-m-d 00:00:00",strtotime($dat)); $t_e = date("Y-m-d 23:59:59",strtotime($dat)); $task_to_do[$x]["start_date"] = $t_s; $task_to_do[$x]["end_date"] = $t_e; $i = $i+$dif; $x++; } echo "
";
   print_r($task_to_do);

Friday 11 July 2014

How to calculate no of working days in php?

function getWorkingDays($startDate, $endDate){
   $begin=strtotime($startDate);
   $end=strtotime($endDate);
   if($begin>$end){
     echo "tartdate is in the future! 
"; return 0; }else{ $no_days=0; $weekends=0; while($begin<=$end){ $no_days++; // no of days in the given interval $what_day=date("N",$begin); if($what_day>6) { // 6 and 7 are weekend days $weekends++; }; $begin+=86400; // +1 day }; $working_days=$no_days-$weekends; return $working_days; } }

Friday 20 June 2014

Dynamically Date Range reset in jQuery UI date picker

Removes the datepicker functionality completely. This will return the element back to its pre-init state. 
Code :
$( "#start_date" ).datepicker( "destroy" );
$( "#end_date" ).datepicker( "destroy" );

Thursday 19 June 2014

jQuery Date picker ui select date range picker

Jquery UI Datepicker select date range between Two days
$( "#start_date" ).datepicker({
   minDate: new Datenew Date(1999, 10 - 1, 25),
   maxDate: new Datenew Date(1999, 10 - 1, 30),
   defaultDate: "+1w",
   changeMonth: true,
   numberOfMonths: 2,
   onClose: function( selectedDate ) {
      $( "#end_date" ).datepicker( "option", "minDate", selectedDate );
   }
});

$( "#end_date" ).datepicker({
   minDate: new Datenew Date(1999, 10 - 1, 25),
   maxDate: new Datenew Date(1999, 10 - 1, 30),
   defaultDate: "+1w",
   changeMonth: true,
   numberOfMonths: 2,
   onClose: function( selectedDate ) {
      $( "#start_date" ).datepicker( "option", "maxDate", selectedDate );
   }
});

Wednesday 24 April 2013

PHP Date functionality

<?php echo $date= date('Y-m-d h:i:s'); echo "
"; echo $validity=30; echo "
"; echo date ("Y-m-d h:i:s", mktime (date('h'),date('i'),date('s'),date('m'),date("d")+$validity,date('Y'))); ?>