This question already has answers here:
Using DateTime::diff() to return days between two dates
(2 answers)
Closed 4 years ago.
I calculate the difference of to queried dates. When the difference is lower than one month everything is correct. Here a example of a wrong result:
Query output:
$row['start'] = '2018-08-06';
$row['end'] = '2018-09-26';
Code:
$start = new DateTime($row['start']);
$end = new DateTime($row['end']);
$days = $start->diff($end)->format("%d");
Output:
$days = 20;
The DateTime difference is correct and the error only comes in when you are formatting the output. %d is the day of the month, not the total days in the DateInterval. These two datetimes are 1 month and 20 days apart. So %d only shows the 20 days part. %a should get you the total number of days.
A full guide to the format can be found here:
http://php.net/manual/en/dateinterval.format.php
Related
This question already has answers here:
Calculate months, years and days between two given dates as timestamp [duplicate]
(4 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 2 years ago.
Here is my code:
<?php
$userDob = $result['DoB'];
//Create a DateTime object using the user's date of birth.
$dob = new DateTime($userDob);
//We need to compare the user's date of birth with today's date.
$now = new DateTime();
//Calculate the time difference between the two dates.
$difference = $now->diff($dob);
//Get the difference in years, as we are looking for the user's age.
$age = $difference->y;
//Print it out.
echo $age;
?>
I want to output something like 50 Years, 6 Months, 20 Days but I don't know how to exactly put it as this method is only outputting number of years only.
Yes php has something built in for that :)
$age_check = "$day-$month-$year";
$today = date("Y-m-d");
$calculate = date_diff(date_create($age_check),date_create($today));
$age = $calculate->format('%y');
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
i have two time like total worked time and total duty time. eg.
$dutyTime= 182:00:00; ("H:i:s")
$workedTime= 178:10:28; ("H:i:s")
my question is that how can i get difference between this two time;
Expected Output
$duration=03:49:32; ("H:i:s")
i tried strtotime() but i takes upto 24hrs.
<?php
$start = date_create('21:21:05'); //start time
$end = date_create('10:20:00'); // end time
$diff = date_diff( $start, $end );
$date= $diff->h.':'.$diff->i.':'.$diff->s;
echo date("H:i:s",strtotime($date));
?>
This question already has answers here:
PHP: producing relative date/time from timestamps
(10 answers)
How do I compare two DateTime objects in PHP 5.2.8?
(8 answers)
Closed 7 years ago.
Here, I want to get hours between two dates.
If I subtract time between two dates, I want 23 hours in a result.
My field's datatype is as datetime.
My code is like,
$usr_check_login = $this -> applib->get_any_field('users_attendance',array('user_id'=>$this->session->userdata('user_id'),'clock_out'=>'0000-00-00 00:00:00'),'clock_in');
$cur_time = date('d-m-Y H:i:s');
$clk_time = date('d-m-Y H:i:s',strtotime($usr_check_login));
$res = $cur_time - $clk_time;
$hours = $res / ( 60 * 60 );
echo $hours."<br>";
echo $cur_time."</br>";
echo $clk_time."</br>";
I am getting result like,
0.00027777777777778
23-01-2016 11:29:19
22-01-2016 11:02:39
Here I want result as 24 hours as the dates are changed.
What can be the solution?
This question already has answers here:
MySQL week calculation between two dates
(2 answers)
Closed 10 years ago.
I am creating a Top 30 Music Chart..
Hw do I get the number of weeks between two mysql formatted datetimes? like
From: 2013-01-15 11:41:14
Current Datetime: 2013-02-25 13:41:14
Using SQL is better, but in php it'll be something like this:
<?php
$datetime1 = new DateTime('2013-01-15 11:41:14');
$datetime2 = new DateTime('2013-02-25 13:41:14');
$interval = $datetime1->diff($datetime2);
$diff = $interval->format('%d');
echo (int)$diff/7;
Subtract the times using strtotime
$difference = strtotime($first_date)-strtotime($second_date);
$weeks = round($difference / 604800 );
<?php
$db_time_a = strtotime('2013-01-15 11:41:14');
$db_time_b = strtotime('2013-02-25 13:41:14');
$seconds_in_between = $db_time_a - $db_time_b;
$hours = (int)($seconds_in_between/60/60);
$minutes = (int)($seconds_in_between/60)-$hours*60;
$seconds = (int)$seconds_in_between-$hours*60*60-$minutes*60;
echo 'The time in seconds in between the two times is: '.$seconds_in_between.' (Hours:'.$hours.', Minutes:'.$minutes.', Seconds:'.$seconds.')';
?>
You can just chunk down the seconds with "remainder" and find out the minutes (and it's remaining seconds) and so on until you hit the time-distance you want.
does the WEEK() function helps you in your query ?
I don't know Mysql very well but lets assume the following :
select WEEK(date1-date2) from mytable where name='Ken';
You can use strtotime() method, then count difference between current time and time you have in your MySQL database.
Try DATEDIFF(date1, date2) function in mysql.
DATEDIFF() will return you the no of date between two given date. Hope you can easily get the weeks from output.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate DateDiff in SQL in Days:Hours:Mins:Seconds format
I have this MySql query:
SELECT DATEDIFF(MAX(hit_date), MIN(hit_date)) / (COUNT(hit_date) - 1) AS hitavg FROM my_table
This returns a value (average days between recorded rows from hit_date column, this column is in TIMESTAMP format, so YY:MM:DD HH:MM:SS)
Assuming that this value is 385.500 (returned from the query), how can I format in PHP this number as 385 Days, "n" Hours (where "n" is the decimal value, in this case 500)?
Thanks in advance to all!
Well, if you only want Days and Hours, then:
$value = 385.500;
$days = (int) $value;
$hours = 24 * ($value - $days);
echo "$days Days, $hours Hours";