Apply time offset to get current time - php

My server has 5 hours difference from my current location time. After I record an event I'd like to display current local time. My time is stored in db as DATETIME value (0000:00:00 00:00:00).
I can get offset time in seconds with PHP like this:
$offset = date('Z');
So, the time from db will be:
$time = '2016-03-31 01:40:13';
But if my time is offset by -18000 sec, i.e. -5 hrs, the display time should be
2016-03-30 20:40:13
So I've attempted:
echo date('Y:m:d H:i:s', mktime(strtotime($time + $offset)));
But it looks convoluted...

setlocale(LC_TIME,'YOUR_LOCALE'); for strftime formatting
AND
date_default_timezone_set('YOUR_TIMEZONE'); for timezone settings

Related

Check elapsed time in PHP from a MySQL datetime

How can I check how many seconds past the current time to a time in my MySQL database?
Example: The current time right now is 2018-07-21 10:04:20, and the time in my database is 2018-07-20 21:58:40.
I want to get how many seconds past the datetime in my database to the current date time.
You can use the strtotime() to convert your Date-Time to Unixtime an time() to get actual Timestamp:
$timestamp = strtotime("2018-07-21 10:04:20"); //replace with your DB-date
$diff = time() - $timestamp ; //calculate the Difference
echo $diff; //negative number is in the future
Just turn the date into number using strtotime.
$seconds = abs(strtotime($currentDate) - strtotime($dbDate));
I put abs in case of the result is negative.
strtotime returns the difference in seconds between 01-01-1970 and the date in parameter.

converting any date having timezone to UTC

I am working on converting any date having timezone to UTC.
i have offset i wants to convert UTC time from that particular time by adding/subs rating offset like +5300 can any one help me how write PHP code for the same.
I have tried following code :
$d = new DateTime('2010-01-31 20:30:00');
$d->modify('+5300 hours');
echo $d->format('Y-m-d H:i:s'), "\n";`
If the time stamp is 2010-01-31 20:30:00+5300, then you would want to do:
$d = new DateTime('2010-01-31 20:30:00');
$d->modify('-53 hours');
5300 stands for 53 hours, 0 minutes, not 5300 hours. Also you need to subtract that to get back to UTC, it is already ahead of UTC.

Carbon Time Difference of Local Time and Server Time

I am using Laravel 4 and trying to calculate the difference of the Time on Server and the Time Save in Database in hours. Here is the code:
Time on Server=timeNowOnServer // 2016-02-03 19:05:43
Time saved in Database = setTime // 18:00:00
$timeNowOnServer = null;
$setTime = null;
$timeZone = $company->time_zone;
$company->save();
// My Time
$hour = substr($company->emp_auto_logout_time, 0, 2);
$setTime = Carbon::createFromTime($hour);
// Server Time Same as Mine
$timeNowOnServer = Carbon::now();
$timeNowOnServerSameAsMine = $timeNowOnServer->setTimezone($timeZone);
// Time Differnce Between SetTime and TimeOnServerSameAsMine
$timeDiff = $setTime->diffInHours($timeNowOnServerSameAsMine, $abs = false);
If Server Time is 14:00:00 for example and Set Time is 19:00:00. My time zone is GMT +5 Pakistan.
What I'm doing is, adding setting Server Time according to user time by giving time zone in the 3rd last line of the above given code.
When I use diffInHour it instead of giving me 0 it gives me -5 or 5. It means it's useing UTC (Server Time) and that's why give wrong difference in hours.
Can someone please let me know where I'm wrong?
You're trying to diff time in different time zones. A time difference (interval) is an absolute value, it doesn't depend on time zone. One second is always one second, be it UTC or Europe/Moscow.
Try without time zone (to be correct - using default server's time zone)
$timeDiff = $setTime->diffInHours($timeNowOnServer, $abs = false);
Always calc intervals using timestamps or the same time zone (doesn't matter exactly which one) for both DT values.

Get current time in PHP like 12:33:00

I am trying to get current time in PHP so that I can store this in the Database.
When I do
$currentTime = time();
or
$currentTime = time('hh:ii:ss');
I get a result which is like
1428492931
but I want the time to be the correct format such as
12:33:00
it is date not time function
date('h:i:s');
time — Return current Unix timestamp
Description
int time ( void ) Returns the current time measured in the number of
seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

how to subtract two dates and times to get difference

i have to sent an email when a user register email contain a link that is become invalid after six hours
what i m doing when email is sent i update the db with field emailSentDate of type "datetime"
now i got the curent date and time and has made to the same formate as it is in db now i want to find that both these dates and time have differenc of 6 hours or not so that i can make link invalid but i donot know how to do this
my code is look like this i m using hardcoded value for db just for example
$current_date_time=date("Y-m-d h:i:s");
$current=explode(" ",$current_date_time);
$current_date=$current[0];
$current_time=$current[1];
$db_date_time="2010-07-30 13:11:50";
$db=explode(" ",$db_date_time);
$db_date=$db[0];
$db_time=$db[1];
i do not know how to proceed plz help
<?php
//$now = new DateTime(); // current date/time
$now = new DateTime("2010-07-28 01:11:50");
$ref = new DateTime("2010-07-30 05:56:40");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
prints 2 days, 4 hours, 44 minutes
see http://docs.php.net/datetime.diff
edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like
... WHERE key='7gedufgweufg' AND expires<Now()
Many rdbms have reasonable/good support for date/time arithmetic.
What you can do is convert both of your dates to Unix epoch times, that is, the equivalent number of seconds since midnight on the 31st of December 1969. From that you can easily deduce the amount of time elapsed between the two dates. To do this you can either use mktime() or strtotime()
All the best.
$hoursDiff = ( time() - strtotime("2010-07-30 13:11:50") )/(60 * 60);
I'd rather work with a timestamp: Save the value which is returned by "time()" as "savedTime" to your database (that's a timestamp in seconds). Subtract that number from "time()" when you check for your six hours.
if ((time() - savedTime) > 6 * 3600)
// more than 6h ago
or
"SELECT FROM table WHERE savedTime < " . (time() - 6 * 3600)
This might be the solution to your problem -> How to calculate the difference between two dates using PHP?

Categories