How to add 30 mins to current timestamp? [duplicate] - php

This question already has answers here:
adding 30 minutes to datetime php/mysql
(6 answers)
PHP Adding 15 minutes to Time value
(9 answers)
Closed 2 years ago.
So basically I need to add 30mins to the current time and send it as a time stamp in database.
date_default_timezone_set('Asia/Hong_Kong');
$today = date("H:i:s");
I.E i need current time : 10:00 sent time to database 10:30.
I know the codes in sending it to the database i just want to know how to add certain mins or seconds to the current time.

You would use the database time for this:
now() + interval 30 minute
You could set a column with the value as:
update t
set col = now() + interval 30 minute
where id = 1;

Pretty simple, but go with the database solution if possible:
$today = date("H:i:s", strtotime(“+30 minutes”);

Related

Check if datetime is older than 5 days [duplicate]

This question already has answers here:
SELECT all records that are 30 days old
(4 answers)
Closed 1 year ago.
So I am going to make a cron job in cPanel that runs every day, but not sure how the query should look like. I have a datetime column with this type of value: 2021-04-06 14:12:06. How can select from table where datetime column is older than 5 days?
You can use date arithmetic. Assuming you mean "day" without the current time value:
where datetime_col < curdate() - interval 5 day
If you want the reference to be the current time rather than the current date:
where datetime_col < now() - interval 5 day

Return current date + 7 days in timestamp form [duplicate]

This question already has answers here:
Return current date plus 7 days
(10 answers)
Closed 2 years ago.
So I have the below property where I'm passing in a time()-86000 value, but what would be the best call to generate a timestamp from the time the class method executes and then adds 7 days to that.
Here is what I got:
$profile->set_picture_expiration(time()-86000)
Would using time()-86000 be the right call? I'd like to write it to the DB in timestamp format from the current time + 7 days.
First of all, if you need +7 days, why are you using minus?
Next, 7 days are 7*24*60*60 = 604800 seconds, not 86000.
Finally, the easiest way to get the timestamp for such relative dates is using the strtotime function. In your specific case it would be strtotime('+7 days').
$profile->set_picture_expiration(strtotime('+7 days'));

How to divide time between particular minutes in php [duplicate]

This question already has answers here:
display hours in half hour range
(3 answers)
Closed 3 years ago.
I have Two fields like time-from and time-to and time-took for particular people so my job is
ex: time-from: 10:00:00
time-to : 1:20:10
time-took : 30 (min)
So i want to display the order like that:
10:00:00 , 10:30:00 11:00:00, 11:30:00, 12:00:00, 12:30:00, 1:00:00, 1:20:10
If you convert the times into a time stamp using the PHP function "mktime", you then have the number of seconds difference between the dates. If the date is irreverent, then you could just use the same day for all of the times.
// The time to get the number of seconds from.
$tmpStr = "01:10:20";
// Break the string into components.
$timeArray = split(":", $tmpStr);
// The date element is redundant but needs to be set.
$timeInSeconds = mktime ($timeArray[0], $timeArray[1], $timeArray[2], 1, 1, 1970);
Now having the time in seconds from midnight allows you to order them accordingly and to also compare two times to find out the difference.

Minutes used from time difference even fraction of a minute in php [duplicate]

This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 4 years ago.
I have 2 outputs from following code:
$start_time = $dt->format('Y-m-d H:i:s');
$end_time = $dt->format('Y-m-d H:i:s');
start_time - 2018-09-27 17:42:01
end_time - 2018-09-27 17:42:16
How can i get the difference in minutes, even if the time diff is below 1 minute, like example 0.5 minutes (30 seconds).
I require only minutes value, even if it is part of a minute. How is it possible in php using time functions?
Thanks in advance.
$differenceInMinutes = (strtotime($end_time) - strtotime($start_time))/60
This converts your 2 dates into a unix time first by using strtotime() (PHP documentation). The unix time is always in seconds. Then when we subtract them from each other we get the difference in seconds between the two dates. To get from seconds to minutes you can of course divide by 60.

Subtract seconds from a specific given time PHP [duplicate]

This question already has answers here:
Subtract one second from a given time
(2 answers)
Closed 8 years ago.
I writing a code for subtract seconds from a time using php. i have date which assigned to variable , i need to subtract seconds from that date.
$date="2014-03-16 17:40:27";
echo date("Y-m-d H:i:s", strtotime($date) - strtotime("-600 seconds"));
but this gives me dates on 1970S, i search everhere and didn't found a answer which matched for my question. can anyone help me to fix this little code
strtotime() gives you a timestamp in seconds. Don't make another timestamp to subtract from it, just take 600 from it:
echo date("Y-m-d H:i:s", strtotime($date) - 600);
//2014-03-16 17:30:27

Categories