This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
I am getting $status_date from mysql database and when I echo $status_date it looks like this -> 2016-01-08 22:18:14. I am setting $now and when I echo it looks like this -> 2016-01-08 22:43:27. I want to subtract $status_date from $now. Here is my code:
$now = date("Y-m-d H:i:s");
$status_date = date("Y-m-d H:i:s",strtotime($row['status_date']));
$x = $now-$status_date;
$x returns this -> 0
Why is it not subracting?
Dates don't subtract like that. Convert both dates to time, subtract, and convert back to a date.
$now = strtotime(date("Y-m-d H:i:s"));
$status_date = strtotime($row['status_date']);
$x = date($now-$status_date);
However if you're trying to get the time between the dates consider the moment library.
https://github.com/fightbulc/moment.php
You can also use something like:
$now = new DateTime();
$status_date = new DateTime($row['status_date']);
$diff = $now->diff($status_date);
Then you can use any of the dateInterval properties or functions with $diff.
Documentation: http://php.net/manual/es/datetime.diff.php
http://php.net/manual/es/class.dateinterval.php
according to http://php.net/manual/en/datetime.diff.php
check this code i wrote here :
https://eval.in/499594
<?php
$strStart = '2015-01-08 22:18:14'; // status date
$strEnd = 'now';
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
$dteDiff = $dteStart->diff($dteEnd);
print $dteDiff->format("%Y-%m-%d %H:%I:%S");
thre result is smth like : 01-0-0 00:38:42
Related
This question already has answers here:
DateTime with microseconds
(10 answers)
Closed 4 years ago.
I want to convert the current date and time with special timezone to decimal(16,4) in PHP.
That's my code:
$gmtTimezone = new DateTimeZone('Europe/London');
$myDateTime = new DateTime("now" , $gmtTimezone);
$time = $myDateTime->format('U');
but It returns a decimal(16) number in GMT and shows 1549351821 but I want something like that:
1549351821.1589
use this code:
$gmtTimezone = new DateTimeZone('Europe/London');
$myDateTime = new DateTime('now', $gmtTimezone);
$time = $myDateTime->format('U');
$formatted_num = number_format($time, 4);
echo $formatted_num;
output: 1,549,355,258.0000;
I have on function which passing some parameter the like
everyWeekOn("Mon",11,19,00)
I want to compute the difference between the current day (e.g. 'Fri')
and passed parameter day i.e. Mon.
The output should be:
The difference between Mon and Fri is 3
I tried it like this
$_dt = new DateTime();
error_log('$_dt date'. $_dt->format('d'));
error_log('$_dt year'. $_dt->format('Y'));
error_log('$_dt month'. $_dt->format('m'));
But know I don't know what to do next to get the difference between the two days.
Note that this question is different from How to calculate the difference between two dates using PHP? because I only have a day and not a complete date.
Just implement DateTime class in conjunction with ->diff method:
function everyWeekOn($day) {
$today = new DateTime;
$next = DateTime::createFromFormat('D', $day);
$diff = $next->diff($today);
return "The difference between {$next->format('l')} and {$today->format('l')} is {$diff->days}";
}
echo everyWeekOn('Mon');
$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';
You can find no. of days in two days by using this code
<?php
$today = time();
$chkdate = strtotime("16-04-2015");
$date = $today - $chkdate;
echo floor($date/(60*60*24));
?>
Please use this may this help you
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
Hello i have date format d/m/Y and i like to change it to Y-m-d.
i use this code:
$check_in = '31/12/2014';
$check_out = '2/01/2015';
$md = explode("/", $check_in); // split the array
$nd = $md[2]."-".$md[1]."-".$md[0]; // join them together
$check_in_new = date('Y-m-d', strtotime($nd));
$mmd = explode("/", $check_out); // split the array
$nnd = $md[2]."-".$mmd[1]."-".$mmd[0]; // join them together
$check_out_new = date('Y-m-d', strtotime($nnd));
and its work fine, but if i try to convert 02/01/2015 (year 2015) the result is 2014-01-02
its convert the year to 2014
any help???
I suggest utilize DateTime class in this case, and provide proper format in it with createFromFormat, instead of exploding those strings:
$check_in = '31/12/2014';
$check_out = '2/01/2015';
$check_in_new = DateTime::createFromFormat('d/m/Y', $check_in);
$check_out_new = DateTime::createFromFormat('d/m/Y', $check_out);
echo $check_in_new->format('Y-m-d') . '<br/>';
echo $check_out_new->format('Y-m-d') . '<br/>';
Ref: http://php.net/manual/en/class.datetime.php
If you want to continue the way you are doing right now, you can easily do it by the following way:
$check_in = '31/12/2014';
$check_out = '2/01/2015';
$check_in_new = implode('-',array_reverse(explode('/',$check_in)));
$check_out_new = implode('-',array_reverse(explode('/',$check_out)));
But there is a better way to do it:
//Set format
$format = 'Y-m-d';
$dto = new DateTime(strtotime($check_in));
$check_in_new = $dto->format($format);
This question already has an answer here:
Difference between 2 dates in seconds [duplicate]
(1 answer)
Closed 8 years ago.
date_default_timezone_set('America/New_York');
$search_date = '2012-12-19 13:22:00';
$right_now = date('Y-m-d H:i:s');
$search_date = new DateTime($search_date);
$right_now = new DateTime($right_now);
$interval = $search_date->diff($right_now);
echo $interval->format('%R%s seconds');
This displays how many seconds are different between the search date and right now.
I would expect it to return more than a two digit value because there is more than a 99 second difference between the two dates, so I am not sure what I am doing wrong.
Alternatively, with very little change to your original code:-
date_default_timezone_set('America/New_York');
$search_date = new DateTime('2012-12-19 13:22:00');
$right_now = new DateTime();
$seconds = $right_now->getTimestamp() - $search_date->getTimestamp();
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
php date compare [duplicate]
(2 answers)
Closed 9 years ago.
I need to compare two dates
d1= 12-11-01(YY-MM-DD) and d2=2008-05-02(YYYY-MM-DD)
comparison d2 <= d1
It is not working as per requirement
Here please note that I need to compare dates with different date formats.
Try with:
$d1 = strtotime('12-11-01');
$d2 = strtotime('2008-05-02');
if ( $d2 <= $d1 ) {}
DateTime is better than strtotime() because it can account for timezones and daylight savings time
$dt1 = new DateTime('12-11-01');
$dt2 = new DateTime('2008-05-02');
if ( $dt2 <= $dt1 ) {
}
See it in action
Do this :
if(strtotime('12-11-01') <= strtotime('2008-05-02'))
{
// do your task here
}
Try this
if(strtotime('12-11-01') <= strtotime('2008-05-02'))
{ }
Like below,
if(strtotime($date1) < strtotime($date2)) {
// do something
}
Research it further here
http://php.net/manual/en/function.strtotime.php
You can use this code:
$d1 = strtotime($date1);
$d2 = strtotime($date2);
if($d2 <= $d1){
// $date2 is before $date1
}