i use the following set of functions for handling mysql dates, maybe they can help you:
function sqlArray[$date, $trim=true] {
$result = array[];
$result['day'] = [$trim==true] ? ltrim[substr[$date,8,2],'0'] : substr[$date,8,2];
$result['month'] = [$trim==true] ? ltrim[substr[$date,5,2],'0'] : substr[$date,5,2];
$result['year'] = substr[$date,0,4];
$result['hour'] = substr[$date,11,2];
$result['minutes'] = substr[$date,14,2];
return $result;
}
function sqlInt[$date] {
$date = sqlArray[$date];
return mktime[$date['hour'], $date['minutes'], 0, $date['month'], $date['day'], $date['year']];
}
function difference[$dateStart, $dateEnd] {
$start = sqlInt[$dateStart];
$end = sqlInt[$dateEnd];
$difference = $end - $start;
$result = array[];
$result['ms'] = $difference;
$result['hours'] = $difference/3600;
$result['minutes'] = $difference/60;
$result['days'] = $difference/86400;
return $result;
}
in your case it should be something like:
$dateplayed = '2011-01-17 11:01:44';
print_r[difference[$dateplayed, date['Y:m:d']]];
hope it works :D
[PHP 4, PHP 5, PHP 7, PHP 8]
microtime — Return current Unix timestamp with microseconds
Description
microtime[bool $as_float
= false
]: string|float
For performance measurements, using hrtime[] is recommended.
Parameters
as_float
If used and set to true
, microtime[] will return a float instead of a string, as described in the return values section below.
Return Values
By default, microtime[] returns a string in the form "msec sec", where sec
is the number of seconds since the Unix epoch [0:00:00 January 1,1970 GMT], and msec
measures microseconds that have elapsed since sec
and is also expressed in seconds as a decimal fraction.
If as_float
is set to true
, then microtime[]
returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.
Examples
Example #1 Timing script execution
Example #2 microtime[] and REQUEST_TIME_FLOAT
See Also
- time[] - Return current Unix timestamp
- hrtime[] - Get the system's high resolution time
By default there's no method available on the DateTime
or DateInterval
class to get the difference between two DateTime
objects in seconds. Therefore, you can either get the difference of the timestamps of the two dates or calculate the number of seconds yourself:
Comparing Timestamps to Get Number of Seconds Between Two Dates
We can simply compare timestamps of two dates to get the difference in seconds. For example:
$start = new DateTime['2011-12-31 00:00:00']; $end = new DateTime['2021-01-01 00:00:00']; echo $end->getTimestamp[] - $start->getTimestamp[]; // output: 284169600
Please note that comparing timestamps could lead to problems with dates before 1970 and after 2038.
Calculating Number of Seconds Between Two Dates
You can calculate the number of seconds between the two dates in the following way:
$start = new DateTime['2011-12-31 00:00:00']; $end = new DateTime['2021-01-01 00:00:00']; $diff = $start->diff[$end]; $daysInSecs = $diff->format['%r%a'] * 24 * 60 * 60; $hoursInSecs = $diff->h * 60 * 60; $minsInSecs = $diff->i * 60; $seconds = $daysInSecs + $hoursInSecs + $minsInSecs + $diff->s; echo $seconds; // output: 284169600
The calculation works in the following way:
- Using the
%r
formatting character, adds the minus sign when the result is negative; - Using the
%a
formatting character gives us the total number of days between two dates; - Using the
%r%a
format together, we can get the negative/positive number of days. Using that, we can convert the number of days into seconds by multiplying it with hours in a day, minutes in an hour, and seconds in a minute [i.e.days * 24 * 60 * 60
]; - The calculation in the last step does not take into account the hours, minutes and seconds in the date difference [as they're not included in the
%a
format]. To include those, we simply use the hour, minute and second properties available on theDateInterval
object and convert them into seconds as shown in the example above.
Hope you found this post useful. It was published 01 Jan, 2021. Please show your love and support by sharing this post.