Could someone assist me on a timestamp issue.. how can I subtract 2 minutes from this timestamp?
echo 'Settings from database (octopus_import_employees):';
$settings = get_settings('octopus_import_employees');
var_dump($settings);
echo 'Timestamp in human format (Started timestamp):';
$started = date("Y-m-d H:i:s", $settings['started']);
var_dump($started);
var_dump($settings); gets an unix timestamp such as: 342534534
var_dump($started); converts it to a readable format such as: 2019-11-08 05:08:58.
All help would be appreciated.
Timestamps are in seconds so you can subtract 120 seconds (i.e. 2 minutes) from it
$time = $settings['started'] - 120;
echo 'Timestamp in human format (Started timestamp):';
$started = date("Y-m-d H:i:s", $time);
var_dump($started);
#YasinPatel solution is definitely the simplest for your situation. In situations where you don't have a unix timestamp input, one of these methods might be easier to use.
You could create a DateTime object from your timestamp using date_create_from_format and subtract 2 minutes from it, using either sub or modify:
$started = date_create_from_format('U', $settings['started']);
$started->sub(new DateInterval('PT2M'));
echo $started->format('Y-m-d H:i:s');
or
$started = date_create_from_format('U', $settings['started']);
$started->modify('-2 minutes');
echo $started->format('Y-m-d H:i:s');
Demo on 3v4l.org
Related
1532131481886863
I tried the code below and don't work it gives me wrong date , i think has something to do with the amount of digits
$epoch = 1532131481886863;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
The only thing I can think of is that the last six digits is microseconds.
So splitting the number will give you correct date and time.
echo date("Y-m-d H:i:s", substr($epoch, 0,-6));
// Optionally you can echo the microseconds also.
echo " " . substr($epoch, -6);
https://3v4l.org/QDQnc
<?php
// Divide by 1000*1000 because given number is epoch with microseconds
// but the DateTime expects time in seconds
// By dividing we will get 1532131481.886863
// We dont need the microseconds so we cast (int) on it to get 1532131481
$epoch = (int)((1532131481886863 /1000) /1000);
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
I have tried to solve it by extracting the numeric part and then parsed it using date function. But it shows me some old date which I guess is not correct.
$datef = "1490914800000+0100";
$adada = date('Y-m-d H:i:s', $datef);
// Gives date 1987-10-13 18:31:28 which is an old date. Please suggest.
One approach, well-covered by this SO question, is to use the DateTime() function to convert time in seconds since epoch to a date, and then display this date using format(). But there are two caveats with your data. First, you appear to have milliseconds since the epoch, which needs to be converted to seconds. Second, you also have a timezone shift, in hours, tagged to the end. I split your $datef string into two parts, epoch and timezone, then arrive at the number of seconds since epoch.
list($epoch, $timezone) = explode('+', $datef);
$epoch = ($epoch / 1000) + (substr($timezone, 0, 2)*60*60) +
(substr($timezone, 2, 2)*60);
$dt = new DateTime("#$epoch");
echo $dt->format('Y-m-d H:i:s');
Output:
2017-03-31 00:00:00
Demo here:
PHP Sandbox
The time seems to be in milliseconds.
You can add the timezone shift to the seconds. 1 hour = 3600 seconds.
$milliSeconds = intval("1490914800000");
$seconds = $milliSeconds/1000;
$date = date("Y-m-d H:i:s", $seconds);
I want to get difference between 2 timestamps of format Y-m-d H:i:s in minutes in PHP.
The code used is,
$currentDate = date('Y-m-d H:i:s');
$userLastActivity = date($date);
$timeLapse = (($currentDate - $userLastActivity)/60);
Here, $date is obtained from database.
The value of $timeLapse is 0 in output. Please help.
Try using unix timestamp. Practically it measures the time in seconds from 1/1/1970 and it's a lot easier to use and understand than a php object.
$currentTimestamp = new DateTime()->getTimestamp();
$userLastActivity = date($date)->getTimestamp();
$timeLapse = (($currentDate - $userLastActivity)/60);
You should have the time saved as timestamp on the server too, in that case you could use the $date directly as a number, with no need for a conversion. And also, because it's universal, you can pass it around to javascript or any other language without any worries for conversion
Use strtotime to parse textual datetime into a Unix timestamp and substract $userLastActivity from $currentDate and divide by 60.
See if this helps -
<?php
$currentDate = strtotime(date('Y-m-d H:i:s'));
$date = "2016-10-11 02:40:50";
$userLastActivity = strtotime($date);
echo round(abs($currentDate - $userLastActivity) / 60). " minutes";
?>
For more details :strtotime
Change these 2 lines for a start, as I don't think dividing dates by 60 is gonna work to well.
$currentDate = time();
$userLastActivity = strtotime($date);
That way you have time stamps and not dates (string)
i've got 2 time stamps: $start_time = 1312346227; and $end_time = 1312346466;
and i am trying to substract them to see the time inbetween $end_time = $end_time - $start_time;
and i get 239.
What is the proper way of converting this to a human readable date?
if i try echo date("h:i:s A",$end_time); i get 04:03:59 and it should be 00:03:59
any ideas?
Thanks
If you have PHP 5.3, use the DateInterval class.
Example stolen from the manual page on DateInterval::format():
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
You get addiionl four hours, because of your timezone. Remember that unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC. If you (or your server) are in UTC+4 TZ, then date() will implicitly do a timezone conversion to your local time.
Solution? Use gmdate() instead
You need to set your timezone correctly. http://www.php.net/manual/en/function.date-default-timezone-set.php
I have an app [iphone], that sends to a server some times [using json], so the times look like hh:mm 24 hour format,
the time gets saved in the db as varchar,
I need to calculate the elapsed time = endTime - startTime
but my problem is that I have the time in the db as varchar!, no time stamp,
so how to calculate the elapsed time, with out changing the varchar type of field in my db?,
can I convert this hh:mm to an int? for the operation?, and then showing it again as a hh:mm, possibly to save in other table?
thanks a lot!
so how to calculate the elapsed time, with out changing the varchar type of field in my db?
You can cast it, but you'd be better off having that as a datetime to start with.
cast(endtime as datetime) - cast(starttime as datetime) -- yields an interval
Easy:
$start_time = '11:10';
$end_time = '18:55';
$start_time = explode(':', $start_time);
$end_time = explode(':', $end_time);
$elapsed_time = $end_time[0]*60+$end_time[1]-$start_time[0]*60-$start_time[1];
// in minutes.
$elapsed_hours = floor($elapsed_time/60);
$elapsed_minutes = $elapsed_time-$elapsed_hours*60;
print $elapsed_hours.':'.$elapsed_minutes;
// 7:45
In PHP:
$json_time = '13:10';
$json_format = 'H:i';
$date = DateTime::createFromFormat($json_format, $json_time);
$mysql_format = 'Y-m-d H:i:s';
echo "Format: $mysql_format; " . $date->format($mysql_format) . "\n";
echo $date->getTimestamp();
Yeilds:
Format: Y-m-d H:i:s; 2011-06-08 13:10:00
1307495400