handling time format in mysql and php [duplicate] - php

This question already has answers here:
How to add 5 minutes to current datetime on php < 5.3
(7 answers)
Closed 8 years ago.
I have a mysql table which contains a field named time and has the "time" format. I added the first line into the table manually and its time is "14:55:00".
Now, in my PHP page, users will fill a form and then submit it. Here, I want my php to assign the last register's time value into a variable like $time (i can do that). Then I want this:
$time=$time + 5 minutes
How can I do that?

If you want to update your sql table, you can use the DATE_ADD function:
UPDATE table SET time = DATE_ADD(time, INTERVAL 5 MINUTE)
If you want to add 5 minutes to a timestamp in PHP you can use the function strtotime:
$time = $time + strtotime("+5 minutes");
If your time is a string (ie in format 14:55:00), you can do the following:
$timeAsString = "14:55:00";
$timestamp = strtotime("+5 minutes", strtotime($timeAsString));
$time = date("H:i:s", $timestamp);
echo $time;

$dateMinutes= date("i")+5;
and use the make time function from php

Related

How to get next hour's 15th minute of time from given time in PHP [duplicate]

This question already has answers here:
Adding minutes to date time in PHP
(13 answers)
Closed 1 year ago.
Is there a way of getting the next hour's 15th minutes from given time?
For Example
if input is 2021-08-26 12:00:37 then output should be 2021-08-26 12:15:00
if input is 2021-08-26 12:30:37 then output should be 2021-08-26 13:15:00
Use Carbon
It is installed by default in Laravel.
$date = Carbon::createFromFormat('Y-m-d H:i:s', '2021-08-26 12:00:37');
$newDate = $date->addMinutes(15)->format('Y-m-d H:i:s');
You can convert the date to a timestamp and then add it the equivalent of 15 minutes in seconds (15 * 60) and then convert it back to a date
I think this is what you looking for...
$start = '2018-05-21 20:24:45';
echo date('Y-m-d H:i:s',strtotime('+15 minutes',strtotime($start)));

Difference between two timestamp in hh:mm:ss format [duplicate]

This question already has answers here:
calculate the difference between 2 timestamps in php
(7 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
How to get time difference in minutes in PHP
(21 answers)
Closed 3 years ago.
I have two timestamp like below format in my database.
$time1 = 4/19/2019 12:21:01 AM
$time2 = 4/19/2019 12:22:50 AM
I want get difference between this two timestamp like
12:21:01
Let me know if someone can give me idea/solution for do it using php.
You can get the difference as
$time1 = '4/19/2019 12:21:01 AM';
$time2 = '4/19/2019 8:15:01 PM';
$start = new DateTime($time1);
$end = new DateTime($time2);
$diff = $start->diff($end);
print $diff->format("%H:%I:%S");
For more details PHP Manual
So you should have mysql select query like this:
Select time1,time2,time_format(abs(timediff(time1,time2)), "%H:%i") as diff From table1
You can change time format, follow this link for time_format function

PHP unix timestamp compare [duplicate]

This question already has answers here:
Does PHP time() return a GMT/UTC Timestamp?
(5 answers)
Closed 7 years ago.
I have an event date & time stored in my database that is being saved as a PHP unix timestamp. Im trying to check if today is greater than the event date and time.
The problem is - i'm trying to check it using local time (America/New_York), but im getting 2 different time zones. time() is displaying in EST and my database is displaying in UTC
Is there any way to check it correctly?
My Event Date from database:
1450447200 (December 18, 2015 2:00pm)
Im trying to compare it with php time()
am I maybe doing this wrong?
*UPDATE - ANSWER*
as per this answer I ended up doing this:
$utc_str = gmdate("M d Y H:i:s", time());
$today = strtotime($utc_str);
$event_datetime = $Event_timestamp;
date_default_timezone_set("America/New_York");
$utc_str_event = gmdate("M d Y H:i:s", $event_datetime);
$event_date = strtotime($utc_str_event);
if($today >= $event_date){
//Do Something
}
You probably have wrong date on server(s). time() always should return epoch (epoch = UTC).
btw. saving date as int on database it isn't best practice
I don't know if this is correct but have you tried using date_default_timezone_set

php: how to add minutes to timestamp [duplicate]

This question already has answers here:
PHP: add seconds to a date
(9 answers)
Closed 8 years ago.
I am trying to figure out how to easily add minutes to a timestamp in php.
I can do this easily in SQL such as select now() + interval '60 minutes' or using the DATEADD function, however I am not sure how to easily do this in PHP.
I have a date variable that I call as:
$date = date("Y-m-d H:i:s");
And now I just need to add or subtract time from this.
Can anybody help?
Thanks!
You could use strtotime() like this:
$date = date('Y-m-d H:i:s', strtotime('now +60 minutes'));
Which would give you the date 60 minutes in the future formatted the way you'd like.
If you have the timestamp is like this
$tmt += 2 * 60 * 1000;
$date= date("Y-m-d H:i:s", $tmt);
Try this

PHP - Get timestamp X hours from now [duplicate]

This question already has answers here:
php string in a date format, add 12 hours
(4 answers)
Closed 8 years ago.
I'd like to take a number of hours imputed in a text box and get a timestamp back so I can create a countdown timer.
The countdown is fine so ignore how that is done etc. But whats the best way to get a timestamp '48' hours from now. For example user has entered 48?
$hours = 48;
$timestamp = (new DateTime())->modify("+{$hours} hours")->format('U');
You can create a timestamp like this:
<?php
strtotime(date('d-m-Y H:i:s') . "+ 48 hours");
?>
Your looking for strtotime. For example, you can just use strtotime('+48 hours'), and it will return a unix timestamp for that time.

Categories