Subtract seconds from a specific given time PHP [duplicate] - php

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

Related

Get next 2 days not counting current day in PHP [duplicate]

This question already has answers here:
PHP Strtotime without current time?
(2 answers)
Closed 4 months ago.
I have an Unix timestamp like this 1660293621 (2022-08-12 8:40). I want to get next 2 days not counting current date. I expect the result to be 2022-08-15 00:00.
I tried
strtotime("+3 Days", $current_date)
but it returns 2022-08-15 8:40, not 00:00
How can I get that in PHP? Thank you~
$Today=date('y:m:d');
// add 3 days to date
$NewDate=Date('y:m:d', strtotime('+3 days'));
Reference:
Increase days to php current Date()
I figured it out, just add 0:00 will help
$next2days = strtotime("+3 days 0:00", $current_date);

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 add period to only TIME (02:00:00) in PHP [duplicate]

This question already has answers here:
PHP Adding 15 minutes to Time value
(9 answers)
Closed 4 years ago.
I want to add a specific period like 2 hours to only time.
BUT only TIME. There will be no date related issue.
Like my time is 02:00:00
And I want to add 1 hour to this time.
So, the result will be 03:00:00
Is there any PHP built-in function like strtotime()?
I don't want explode related function for this like explode(":", $time)
Is it possible? Please help someone.
Try
$new_time = date("H:i:s", strtotime("+ 1 hours"))
or if you would just like the timestamp:
strtotime("+ 1 hours")

PHP check if 1 day passed [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP:find day difference between two date(“YmdHis”) reture
What I want to do is to get day from database, current date. And check if difference between them > 1 day:
$curdate= date("Y-m-d H:i:s");
$dbdate is value stored in datetime format in db.
$dif=$curdate-dbdate;
How to check if $dif>1 day ??
Assuming the stored date is expressed in the same time zone as the server, you can convert it to a timestamp using strtotime, and compare it to strtotime("-1 day"):
if (strtotime($dbdate) < strtotime("-1 day"))
frobnicate();
You can get just the day from each date.
$day = intval($curdate= date("d"));
This will get the day as an in. Do the same for the time of the data base and you get two integer representing the day. Using that you can calculate how many days have pass.
Beware that the last line should look like this:
$dif = abs($curdate-$dbdate);

Subtracting dates in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate the difference between two dates using PHP?
I have timestamps stored in the format YYYY-MM-DD HH:MM:SS (for example 2010-06-21 20:12:56). What would the best way to check how old the timestamp is? For the moment I am mainly interested in the number of days old.
You can use strtotime to convert the string to a UNIX timestamp, which is in seconds. time() will give you the current UNIX timestamp. Subtract them to get how old the date is in seconds, and divide by 60*60*24 to get it in days
It's also doable using DateTime::diff, although I find the date functions easier than using the classes
$today = strtotime(date('Y-m-d H:i:s'));
$expireDay = strtotime($row['ExpireDate']);
$timeToEnd = $expireDay - $today;

Categories