PHP Convert time from DialogFlow to Time - php

I am getting the time with DialogFlow and the time format is something like "2020-08-28T14:00:00+02:00". I need to get the substraction between a time stored in the data base (format: "H:i:s" -> "12:01:00");
$time = $res2["Results"][0]["time"]; // the time of data base
$time_converted = DateTime::createFromFormat("H:i:s", $time);
$time_end_converted = strtotime("yyyy-MM-dd'T'HH:ii:ss", $params->time_end); //time from dialogflow
//var_dump($time_end_converted);
$interval = abs($time - $time_end_converted);
$time_end = date('H:i:s', $interval);
//var_dump($time_end);
But the difference between both times are not correct. I think the time_end_converted has not got a correct format. How can i convert the time from DialogFlow to a time with the format "H:i:s"??
Thanks!

At the end, I realised that the format is not needed to create DateTime so I finally do this:
$time = $res2["Results"][0]["time"];
$time_converted = DateTime::createFromFormat("H:i:s", $time);
$time_end_converted = new DateTime($params->time_end);
$interval = $time_converted->diff($time_end_converted);
$time_end = $interval->format("%H:%I:%S");
Thank you everyone for helping me!

Related

PHP - Difference between two times

I've created a timing system for a charity race. I'm trying to find the difference between the start time and the finishers time using PHP. I'm not sure I'm recording the times correctly, but this is the start time i just recorded...
20180808180653
And this is a finisher time...
20180808180654
The difference between them is roughly 1 hour 24, but when i use...
date('h:i:s', $finshTime-$startTime)
I get 03:24:20 not 01:34:20.
Can someone please help?
The date method accepts as "integer Unix timestamp". You are supplying instead a number of seconds (1 in your example).
$start = '20180808180653';
$end = '20180808180654';
$diff = $end - $start;
var_dump($diff); //1
$d = date('h:i:s', $$diff);
var_dump($d); //04:00:01
//the above is wrong. You need to try something like the code below
$dStart = new DateTime($start);
$dEnd = new DateTime($end);
$interval = $dStart->diff($dEnd);
var_dump($interval->format('%h:%i:%s'));
I'd be leery using a string representation of a datetime that looks like that. Convert the whole thing into a date format that makes sense like yyyy-mm-dd hh:mm:ss, or a valid unix time stamp.
Your first approach isn't that far off, you just need to use a strtotime function. I'd guarantee that you can first make an accurate Date or Unix time representation of those strings you are using. Rest should fall into place.
First check if the type of $finshTime and $startTime are integer.
you can use get variable type:
gettype($startTime);
if this is the case try this with ():
$diff_date = date('h:i:s', ($finshTime - $startTime) );
if $startTime and $finshTime are string try this:
$diff_date = date('h:i:s', (strtotime($finshTime) - strtotime($startTime)) );

Calculate difference between time in DB and Current Time

First off - I am using the MySQLi Procedural method of using MySQL via PHP
I am trying to create a way click a button to set a "timer". From what I understand, the best way to do this (on a dynamic timer - IE: 5 minutes from click) is to calculate what the time() would be 5 minutes from "now".
I want to save this to a DB in case the user disconnects from the page and reconnects (username/password login). This way, when they log back in, it would keep their remaining time.
Now for the code:
I am using this to pull my DT variable from SQL (YYYY-MM-DD HH-MM-SS):
$expirationTime = new DateTime();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$expirationTime = $row['workTimer'];
}
}
$time = strtotime($expirationTime);
$expirationTime = date("Y-m-d H:i:s", $time);
Then I pull the current time:
$currentTime = date('Y-m-d H:i:s');
I have done echos on both of these and they are displaying correctly. What I want to do now is figure out the difference. I have tried multiple ways of doing this, but nothing seems to work. Most recently I did:
$countdown = $currentTime->diff($expirationTime);
$countdown = $countdown->format("Y-m-d H:i:s", $countdown);
with an error of Fatal error: Call to a member function diff() on a non-object in...
What might be the problem?
Thanks!
I have updated some of the codes per the comment's suggestions to:
$currentTimeT = date('Y-m-d H:i:s');
$currentTime = strtotime($currentTimeT);
$time = date("Y-m-d H:i:s", $expirationTime);
$expirationTime = strtotime($expirationTime);
$countdown = $currentTime->diff($expirationTime);
$countdown = $countdown->format("Y-m-d H:i:s", $countdown);
echo "C ".$currentTime."<br/> E ".$expirationTime."<br/>D ".$countdown;
Same Error.
It is likely the least elegant solution but you could cut your to timestrings both into "d,m,y,h,i,s"-substrings and feed them to http://php.net/manual/de/function.mktime.php. Then you calc the difference of the timestamps.
date_diff — Returns the difference between two DateTime objects
and you are passing non-object to function diff()
See this PHP manual
I solved your issues on my side give it a try:
$time = "2016-09-1 04:45:54";
$time = strtotime($time);
$currentTime = new DateTime(date('Y-m-d H:i:s'));
$expirationTime = new DateTime(date("Y-m-d H:i:s",$time));
$countdown = $currentTime->diff($expirationTime);
$countdown1 = $countdown->format("%H:%I:%S");
print_r($countdown1);die;

Time stored on table is greater than now

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');

Get the closest (in past) time based on a string

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');
}

PHP| arrays with times

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
}

Categories