I have this weird situation.
I'm trying to convert my time stored on a table to time-ago format.
It's working well on my localhost, but not on the server.
For some reason, when calculating the difference between the now and the time on the server, I receive a negative number.
Here is an example:
$time = '2015-01-02 05:52:49'; //Time that is stored on the created cell
$now = time();
$seconds = strtotime($time);
$difference = $now - $seconds;
The output for the above code is -13628.
Timezone is set to date_default_timezone_set('America/New_York');.
What am I missing here?
if the date/time you run this is before '2015-01-02 05:52:49' then the - sign is logical. Try use this instead:
$date1 = new DateTime();
$date2 = new DateTime('2015-01-02 05:52:49');
$now = $date1->getTimestamp();
$seconds = $date2->getTimestamp();
$difference = $now - $seconds;
[SOLVED]
So, the problem was that I've set the timezone to date_default_timezone_set('America/New_York');.
To solve this, I changes this to date_default_timezone_set('UTC');
And now it's working well.
User following code
date_default_timezone_set('UTC'):
instead of
date_default_timezone_set('America/New_York');
Related
Im trying to get the difference between 2 differente dates in minutes, but is not outputting correctly.
Ex:
$then = "2017-01-23 18:21:24";
//Convert it into a timestamp.
$then = strtotime($then);
//Get the current timestamp.
$now = time();
//Calculate the difference.
$difference = $now - $then;
//Convert seconds into minutes.
$minutes = floor($difference / 60);
echo $minutes;
Is outputting 611 minutes, and is wrong since from "2017-01-23 18:21:24" to "2017-01-24 12:36:24" it past much more than 611 minutes. Is my code incorrect?
Try to set your default timezone
date_default_timezone_set('Europe/Copenhagen');
Ofc change Europe/Copenhagen for the one that suits your needs.
If you are using or able to use PHP 5.3.x or later, you can use its DateTime object functionality:
$date_a = new DateTime('2010-10-20 08:10:00');
$date_b = new DateTime('2008-12-13 10:42:00');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%h:%i:%s');
You can play with the format in a variety of ways, and once you have dates in DateTime objects, you can take advantage of a lot of different functionality, for example comparison via normal operators. See the manual for more: http://us3.php.net/manual/en/datetime.diff.php
I've checked your code it works perfectly So if have any doubt see your result
But you got wrong, so to ignore this set your timezone.
I want to create a function that can find the closest time, based in a string of second.
The system will receive an int number that equivalent of second of that time.
PHP must find the closest (in past) date.
Example:
//supose that an anterior script created it at "14-08-25 10:32:30"
//and now it's "14-08-25 10:33:12"
$seconds = 30; // the variable passed from an anterior script
$time_received= date('Y-m-d H:i:s'); // this is the time that I'll receive this
//so, from these 2 variables above, i must find "14-08-25 10:32:30"
Anyone have an idea how to do this?
I have just these variables:
The time right now, that is "14-08-25 10:33:12"
and the $seconds variable.
With these 2, I want to get "14-08-25 10:32:30"
It isn't completely clear to me what you are trying to accomplish, but I'm making a guess by using strtotime():
<?php
$seconds = 30;
$time = strtotime("-" . $seconds . " seconds");
echo date( "Y-m-d H:i:s", $time );
?>
Found.
I'm using this script:
$seconds = 30;
$new_date = new DateTime(date()); //the only two variables that i have
if($new_date->format('s')<$seconds){
$new_date->setTime($new_date->format('H'),$new_date->format('i')-1,$seconds);
$old_date = $new_date->format('Y/m/d H:i:s');
}else{
$new_date->setTime($new_date->format('H'),$new_date->format('i'),$seconds);
$old_date = $new_date->format('Y/m/d H:i:s');
}
I need to compare bentween a time taken from a database to the current time.
$DBtime = "2013-10-29 17:38:55";
this is the format of the arrays in the database.
How can I compare it with the current time?
Im not sure how, but maybe converting DBtime to Unixtime then:
(CurrentUnixTime - dbUnixTime) = x
Or maybe, we can take the 17:38 and compare it somehow with date("G:i");
Thank you! I hope you understand what I mean.
You can transform it into a UNIX timestamp using strtotime and then subtract the current timestamp by it.
$DBtime = "2013-10-29 17:38:55";
$db_timestamp = strtotime($DBtime);
$now = time();
$difference = $now - $db_timestamp;
echo $difference;
This will give you the difference in seconds.
You can convert the DBtime string to a unix timestamp in PHP using strtotime. In MySQL, you can use UNIX_TIMESTAMP when querying the column.
time() - strtotime($DBtime)
$date1 = new DateTime('2013-10-29 17:38:55');
$date2 = new DateTime('2013-11-29 18:28:21');
$diff = $date1->diff($date2);
echo $diff->format('%m month, %d days, %h hours, %i minutes');
$DBtime = "2013-10-29 17:38:55";
// Set whatever timezone was used to save the data originally
date_default_timezone_set('CST6CDT');
// Get the current date/time and format the same as your input date
$curdate=date("Y-m-d H:i:s", time());
if($DBtime == $curdate) {
// They match, do something
} else {
// They don't match
}
How can I compute time difference in PHP?
example: 2:00 and 3:30.
I want to convert the time to seconds then subtract them then convert it back to hours and minutes to know the difference. Is there an easier way to get the difference?
Look at the PHP DateTime object.
$dateA = new DateTime('2:00');
$dateB = new DateTime('3:00');
$difference = $dateA->diff($dateB);
(assuming you have >= PHP 5.3)
You can also do it the procedural way...
$dateA = strtotime('2:00');
$dateB = strtotime('3:00');
$difference = $dateB - $dateA;
See it on CodePad.org.
You can get the hour offset like so...
$hours = $difference / 3600;
If you are dealing with times that fall between a 24 hour period (0:00 - 23:59), you could also do...
$hours = (int) date('g', $difference);
Though that is probably too inflexible to be worth implementing.
Check this link ...
http://www.onlineconversion.com/days_between_advanced.htm
I used this to calculate the difference between server time and the users local time. Grab the hour difference and drop that in a form when the user is registering. I then use it to update the time on the site for the user when they do stuff online.
Once I got it working, I switched this line ...
if (form.date1.value == "")
form.date1.value = s;
to ...
form.date1.value = "<?PHP echo date("m/d/Y H:i:s", time()) ?>";
Now I can compare the user time and the server time! You can grab the seconds and mins as well.
I have two timestamps recorded on a mysql table using php. How can I calculate the difference between these timestamps in hours using php or mysql?
If you actual hours (3600 seconds), it's just a matter of subtracting the timestamps and dividing by 3600:
$hours = ($timestamp2 - $timestamp1)/3600;
$hours = floor($hours); // to round down.
or similar in SQL.
If you want the "logical hours":
$tz = new DateTimezone("Europe/Lisbon"); //replace with actual timezone
$d2 = new DateTime("#$timestamp2");
$d2->setTimezone($tz);
$d1 = new DateTime("#$timestamp1");
$d1->setTimezone($tz);
$hours = $d2->diff($d1)->h;
This makes difference in case there has been a DST change between the two times. Example:
<?php
$ltz = new DateTimezone("Europe/Lisbon");
date_default_timezone_set("Europe/Lisbon");
$ts2 = strtotime("31-Oct-2010 02:30:00");
$ts1 = strtotime("31-Oct-2010 00:30:00");
$d2 = new DateTime("#$ts2");
$d2->setTimezone($ltz);
$d1 = new DateTime("#$ts1");
$d1->setTimezone($ltz);
var_dump(floor($ts2-$ts1)/3600);
var_dump($d2->diff($d1)->h);
gives:
float(3)
int(2)
in php you can just use regular math and then use the date() function to create the datetime represented in hours
Done in PHP, see this: http://php.about.com/od/advancedphp/qt/math_time_php.htm