I am trying to get current time in PHP so that I can store this in the Database.
When I do
$currentTime = time();
or
$currentTime = time('hh:ii:ss');
I get a result which is like
1428492931
but I want the time to be the correct format such as
12:33:00
it is date not time function
date('h:i:s');
time — Return current Unix timestamp
Description
int time ( void ) Returns the current time measured in the number of
seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Related
How can I check how many seconds past the current time to a time in my MySQL database?
Example: The current time right now is 2018-07-21 10:04:20, and the time in my database is 2018-07-20 21:58:40.
I want to get how many seconds past the datetime in my database to the current date time.
You can use the strtotime() to convert your Date-Time to Unixtime an time() to get actual Timestamp:
$timestamp = strtotime("2018-07-21 10:04:20"); //replace with your DB-date
$diff = time() - $timestamp ; //calculate the Difference
echo $diff; //negative number is in the future
Just turn the date into number using strtotime.
$seconds = abs(strtotime($currentDate) - strtotime($dbDate));
I put abs in case of the result is negative.
strtotime returns the difference in seconds between 01-01-1970 and the date in parameter.
My server has 5 hours difference from my current location time. After I record an event I'd like to display current local time. My time is stored in db as DATETIME value (0000:00:00 00:00:00).
I can get offset time in seconds with PHP like this:
$offset = date('Z');
So, the time from db will be:
$time = '2016-03-31 01:40:13';
But if my time is offset by -18000 sec, i.e. -5 hrs, the display time should be
2016-03-30 20:40:13
So I've attempted:
echo date('Y:m:d H:i:s', mktime(strtotime($time + $offset)));
But it looks convoluted...
setlocale(LC_TIME,'YOUR_LOCALE'); for strftime formatting
AND
date_default_timezone_set('YOUR_TIMEZONE'); for timezone settings
If I got a unix time which is e.g
1407050129
How do I get the 12:00AM of that unix day in unix timestamp , means the first minute of the day of that unix time.
Example if i want get today first minute
$today_first_min = strtotime("00:00:00");
Try this:
$that_day = "1407050129";
$that_day_first_min = strtotime(date('Y-m-d', $that_day) . ' midnight');
See demo
An alternate method to arrive at the same result... Unix time is a count of seconds since 1970-01-01 00:00:00 UTC, so each whole day is a multiple of (60*60*24) seconds.
Using this fact you can use the modulus operator (%) to calculate and then remove the remainder (ie. the seconds, hours and minutes) from the day, and get back to the first hours!
date_default_timezone_set('UTC');
$that_date = 1407050129;
$first_hour = $that_date - ($that_date % (60*60*24));
print date('Y-m-d H:i:s', strval($first_hour));
// 2014-08-03 00:00:00
I download event calender from http://www.phpcodeworks.com/pec/installation. I am using PHP 5.3.X therefore browser said F:\xampp\htdocs\msj\functions.php so I replace :
$days = date("t", mktime(0,0,0,$month,1,$year));
with:
$days = date("t",` time(0,0,0,$month,1,$year));
but doing so each date goes 24 hours back as follows.
Image when using $days = date("t", mktime(0,0,0,$month,1,$year));:
Image when using $days = date("t", time(0,0,0,$month,1,$year));:
The mktime() function returns the time in seconds from Unix Epoch (January 1 1970 00:00:00 GMT) to the date and time provided as parameters.
The time() function retuns the time in seconds from Unix Epoch (January 1 1970 00:00:00 GMT) to the moment the function is run. There are no parameters to pass in.
So when browsing for a particular date, you will need to use mktime() instead of time(), time() will constantly return a different number every time you run it. Because of that, your calendar will change every time you view it (even if you are trying to view a particular date).
Hightlight:
mktime() - Time in seconds representing a specified date (see the documentation for the required parameters).
time() - Time in seconds representing now (there are no parameters for this function).
mktime() looks to be the appropriate function for this situation.
I am new to PHP and am writing a PHP script that contains a module which reads the file timestamp out of a file and then compare with system time to see whether the file is older than 5 minutes or not and I am wondering how that can be achieved. I am currently using
$timeStamp = strftime('%c', filectime($this->localPath));
$timeStamp2 = filectime($this->localPath);
The two $timeStamp and $timeStamp2 are different, the 1st one is more human readable
$timeStamp Mon Jun 20 15:17:01 2011
$timeStamp2 1308608221
What does $timeStamp2 mean?
And again, how to see if the file is more than 5 minutes old?
That is Unix timestamp actually (seconds since 1st jan 1970 or EPOCH)
You can use time() function to get current time in same Unix format.
And then subtract both time values to check whether difference is > 300 (5 min) or not.
$timeStamp2 is a UNIX timestamp (the number of seconds passed since 01/01/1970).
You can get the same thing from $timeStamp1 by doing
$timeStamp1 = strtotime($timeStamp1)
and then compare the two values
Answering your question "What does $timeStamp2 mean?"
1308608221 is the number of seconds that have passed since midnight January 1,1970.