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;
Related
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!
Although my question seems can be found the solution on the internet easily. But I've already tried but it's not working.
I've already followed https://www.php.net/manual/en/datetime.diff.php Example #2 DateTime object comparison
or another solution like https://thevaluable.dev/php-datetime-create-compare-format/ Comparing DateTime Objects
But it is still not working.
Here is my code,
$end_time = new DateTime('2020-04-05 23:59:00');
$now = new DateTime('now');
if( $now > $end_time ){
echo 'expired!';
}
It throws the error
Object of class DateTime could not be converted to string.
Edited
I'm using PHP 7.1.23
Here is the solution for your problem. First you have to convert them into Strings then you can use them. I have changed the input date just to show you the result of if condition.
Select your City for time zone First
date_default_timezone_set('Asia/Karachi');
Your Inputs
$input_time = new DateTime('2020-04-01 23:59:00');
$now = new DateTime('now');
Convert them to string
$input_time = $input_time->format('Y-m-d H:i:s');
$now = $now->format('Y-m-d H:i:s');
The result
if( $now > $input_time )
{
echo 'expired!'. '<br>';
}
If it doesn't need to be an actual DateTime object, you could use times instead, which will then compare the same as an integer would.
Eg
$end_time = strtotime('2020-04-05 23:59:00');
$now = time();
if( $now > $end_time ) {
echo 'expired!';
}
How do I remove time from a date in PHP, for example:
20170803173418 I want to take 4 minutes and 13 seconds away and get the new datestamp that would be 20170803173005
What code do I use to get this?
EDIT
I currently have:
$dbTime = $row['aptDeadline']; // This is the appointment end time stored in DB
$dbTime = new DateTime($dbTime);
$currentTime = date("YmdHis"); // This is the current time
$currentTime = new DateTime($currentTime);
$counterTime = $row['aptDeadline']; //This is the time a countdown clock works from inDB
$counterTime = new DateTime($counterTime);
$difference = $currentTime->diff(new DateTime($dbTime)); // Calculate the time between now and the apt time in the db
I now need some code that if the $difference is positive, can take this figure away from the $counterTime stamp
You can use the modify method of the DateTime class in PHP:
<?php
$time = new \DateTime('20170803173418');
$time->modify('-4 minutes')->modify('-13 seconds');
echo $time->format('YmdHis');
This will print the result you want.
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');
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');
}