Getting current time , setting it to a variable that doesnt update [duplicate] - php

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?

Related

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 30 mins to current timestamp? [duplicate]

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”);

Add an hour to a time value on php [duplicate]

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

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

Subtract two dates in PHP / Mysql [duplicate]

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'

Categories