mktime VS time PHP4 VS PHP5 - php

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.

Related

Check elapsed time in PHP from a MySQL datetime

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.

Get current time in PHP like 12:33:00

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).

PHP calculating time difference, result 1 hour wrong

I want to calculate difference of two times and print it in pretty way. I know the difference will never exceed 24 hours, so I tried the following
$time = 7200; //time difference in seconds
date("H:i:s", $time); // should print 02:00
The result is unexpected, it prints 03:00 instead.
What am I doing wrong?
Why not the DateTime::diff??
Here, check PHP.net for DateTime class
Grabbed from php.net datetime diff page:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
//Outputs +2 days
The PHP date() function is meant for formatting absolute time stamps (as in "Friday, 20 July 2012, 15:02 UTC"), not for time differences (as in "3 hours and 5 minutes ago").
In some cases, it is possible to trick date() into producing something that looks like a correctly formatted time difference by setting your time zone to UTC and relying on the fact that the Unix epoch happens to fall on midnight in UTC. However, even then, this will only work for positive time differences of less than 24 hours.
Instead, the correct solution is to use a function designed for formatting time differences, or write one yourself. Here's a simple function to do so:
function format_time_difference ( $delta ) {
$sign = ( $delta < 0 ? "-" : "" );
$delta = abs( $delta );
return sprintf( "%s%02d:%02d:%02d", $sign, $delta / 3600,
($delta / 60) % 60, $delta % 60 );
}
Of course, you can extend this function to e.g. include days in the output if you like.
You should never use date to compute time difference. Firstly it is ugly hack. It was not intended for that purpose. And secondly, it works reliably only when timezone set to UTC.
Now, why it does not work:
PHP function date takes two arguments, format and timestamp, the latter is defined as number of seconds from 1st January 1970 00:00 UTC called unix epoch. So if you call date("H:i", 3600) and your timezone is set to UTC, it will return "01:00", cause it represents time one hour after unix epoch and the epoch was at the midnight.
The problem is, unix epoch was at the midnight only in UTC, not in the other timezones. And this is the source of the incorrect result.

Timestamp intervals processed by date() don't calculate correctly

I have two unix timestamps in my database that I am subtracting to get a time interval in seconds:
$interval = $array["time2"] - $array["time1"]; // When echoed, $interval = 3
However, when I run this $interval through date(), like so:
echo date("g\h i\m", $interval);
these 3 seconds all of a sudden echo to:
7h00m
Does anyone have any idea why date() might be taking these three seconds and stretching them out into a 7 hour interval somehow?
The second argument to date() is a timestamp (seconds since midnight, Jan 1, 1970 GMT). Your interval is probably equating to 7am in your timezone relative to this date.

PHP: Compare file timestamp with system time

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.

Categories