This question already has answers here:
Add 'x' number of hours to date
(13 answers)
Closed 7 years ago.
I have a form that people are filling in and adding appointment times to, im then inputting the form into a sql db. Im then making a sheet and that shows the appointment slot. All that I need to to is add 1 hour to the appointment slot time. I have a variable call $time that is the appointment time I have tried the following:
<?php
$time=strtotime("+3600");
$final=date("H:i",$time);
echo $final;
but it comes back with all different times any ideas how to make this work?
$time=strtotime("+1 hour");
$time=strtotime("+60 minutes");
Strtotime means: String to time, very useful method when working with dates in PHP.
Or like below answer:
$time = time() + 3600;
Note that you should avoid calculation with strings, that is: A number between quotes. (string)"3600" vs (int)3600
Related
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'));
This question already has answers here:
How to Subtract current date time from date time in database - PHP
(4 answers)
PHP Session variable - find time difference
(2 answers)
Closed 3 years ago.
i want to get the current time and set it to a variable . after that i want to get the current time and subtract the variable from it . this way i can get the elapsed time. can anyone help ? Edit: wihtout the use of a database.. is this possible?
$firsttime = time(); #ofcourse this is going to update to current time
#which is my problem
$diff = time() - $firsttime;
echo "<br>";
echo "<br>";
echo "You visited this page ",$diff," seconds ago";
It appears you are running two time() functions one right after the other, therefore the difference is in milliseconds. I believe what you want to do is store a cookie on the browser or store the time in a session (firsttime) and compare that value on the subsequent request.
You may then wish to update that cookie/session to the new time so you can see the time differences between page visits.
On the final line, shouldn't those commas be periods?
This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 7 years ago.
Hi I'm still new in PHP and currently I want to make system about Employee Allowance and have no idea how to start calculate months in php .For example Steve should get allowance for 4 months from now and database stored the result of calculation, so far the coding is:
?php
$date=date_create("2013-03-15");
date_add($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
Months input is from user and stored in database.
I'm not sure I follow your 40 days versus 4 months, but I think you want something like this:
echo date('Y-m-d', strtotime("Now +40 days"));
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
This question already has answers here:
Difference between two dates in MySQL
(14 answers)
Closed 9 years ago.
I've searched but could not find the solution for my case.
One date is recorded at MYSQL,
for example it returns: 2013-10-18 15:42:06 (which format is this ?)
So I need to get the current date (including hours, minutes and seconds).
Then, subtract the MYSQL date - CURRENT date.
The result, i'll set as an jQuery countdown.
Thanks!
This should work:
$seconds_remaining = strtotime('2013-10-18 15:42:06') - time();
Although if you happen to know what timezone the MySQL time corresponds to, then you should append that to the argument passed to strtotime(), e.g., '2013-10-18 15:42:06 GMT'