Maths on PHP Timestamps - php

I have the following block of PHP:
if ($data->timestamp > (time() - 10 * 60) ) {
// data is newer than 10 mins
} else {
// data is older than 10 mins
}
That basically says if the timestamp stored in $data is older than 10 minutes.
I'm not sure how this is worked out exactly, and I've read about the time function here: http://php.net/manual/en/function.time.php
I want to edit it to become every hour instead of 10 minutes.
Can anyone share some advice/resources? e.g. how I would change it to become 1 hour and how exactly this is worked out.

This code line checks if the timestamp saved in the $data is NOT older than 10 minutes.
the time() function gives UNIX tiemstamp of the machine in seconds. If you want to change it to an hour its 60 minutes in 60 seconds 60*60. Your code will be like this:
if ($data->timestamp > (time() - 60*60) )

it is worked out starting from the right to left in seconds. One hour would be
if ($data->timestamp > (time() - 60 * 60);
Formatt
time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
You are multiplying it as you work back 60 multiplied by 60 minutes etc etc

Related

I have a Broadcast system for in site talking to users,Keeps right time up till 24 hours. After the 24 hour max the time speeds up

$time_ago_op = time() - $comments[$c]['bcttime'];
if ($time_ago_op <= 60) { $comments[$c]['time_ago'] = $time_ago_op . " secs ago."; }
if ($time_ago_op >= 61 && $time_ago_op <= (60 * 60)) { $comments[$c]['time_ago'] = CleanNumber($time_ago_op / 60) . " mins ago."; }
if ($time_ago_op >= (1+(60 * 60)) && $time_ago_op <= (60 * 60 * 24)) { $comments[$c]['time_ago'] = CleanNumber($time_ago_op / (60 * 60)) . " hours ago."; }
if ($time_ago_op >= (1+(60 * 60 * 24)) && $time_ago_op <= (60 * 60 * 24 * 7)) { $comments[$c]['time_ago'] = CleanNumber($time_ago_op / (60 * 60 * 7)) . " days ago."; }
unset($time_ago_op);
After it a user posts it keeps the right time in seconds to minutes and hours, Once it hits 24 hours the time speeds up. For example a post that is 28 hours old says 3 days old and such... I am trying to figure out how to get it to keep the correct time and I am not having any luck.. If anyone can help and point out what I have set wrong it would help out a lot. Thanks
The issue is you are using just pure if blocks. You need to use PHP's if else construct.
Further imagine this scenario. The third if block is at the last nano second before it will trigger the fourth if block. The third will fire off, and then immediately fire the fourth, this can/will cause incorrect calculations.
EDIT
Why don't you just do something like this:
date_default_timezone_set("bcttime"); // whatever the correct time zone is
$server_time= date('G:ia');
$comment_time = $server_time - $comments[$c]['bcttime'];
// display the time with formatting

How to get current time elapsed percentage of today? [duplicate]

This question already has answers here:
Work out the percentage of the day that has elapsed
(5 answers)
Closed 9 years ago.
Im trying to get the current time elapsed percentage of todays time. It can be either javascript or php. How can I do that ?
Use a Date object and some arithmetic. Convert the components of the current day into a equivalent unit (such as seconds), find their sum, and divide that by the number of that unit per day.
For example, the following is a solution in JavaScript.
var d = new Date();
var pctDayElapsed = (d.getHours() * 3600 + d.getMinutes() * 60 + d.getSeconds() + d.getMilliseconds()/1000)/86400;
Note that this approach piggybacks on the browser's localization. Your result will depend on the browser's timezone.
I would use the current time to calculate the number of seconds that has elapsed today and then divide that by 86400 (the number of seconds in a day), and of course multiply that by 100 to get it from decimal to percent.
Here is an answer in PHP:
$now=time();
$today=strtotime(date("m/d/Y"));
$seconds=$now-$today;
$day=24*60*60;//seconds in a day;
$percent=$seconds/$day*100;
OR
$hours=date('G')*60*60;
$minutes=date('i')*60;
$seconds=date('s');
$day=24*60*60;//seconds in a day;
$percent=($hours+$minutes+$seconds)/$day*100
<?php
$timestamp = time();
$hours = intval(date("G", $timestamp));
$minutes = intval(date("i", $timestamp));
$seconds = intval(date("s", $timestamp));
$proportion_elapsed = ($hours * 60 * 60 + $minutes * 60 + $seconds) /
(24 * 60 * 60);
printf("%0.4F of the day has elapsed.", $proportion_elapsed);
?>
This works as long as there are 86,400 seconds in a day, which may not be true due to Daylight Savings time or leap seconds.
Round down to the beginning of the day by dividing the current time by 86400, then multiply that integer value by 86400. Then take the difference of the current time (in seconds of course) and then divide 86400 into it. Lastly, multiply by 100.
Edit: mod works more efficiently
Pseudocode:
MidnightDays = (TimeInSeconds % 86400) * 86400;
Percentage = (TimeInSeconds - MidnightDays) / 86400 * 100;

Print out Relative Date / Time from SQL / PHP Datestamp

I have a SQL Datestamp like this: 2012-02-20 21:14:54
How would I print out the relative date and time in PHP?
e.g.
Occured: a few seconds ago
Occured: 4 minutes ago
Occured: 4 hours ago
Occured: Monday Jan 8th, 2012
After the hours I just want to print out the actual date
Found this after two seconds of Google http://www.mdj.us/web-development/php-programming/another-variation-on-the-time-ago-php-function-use-mysqls-datetime-field-type
In general you chose a unit of time like seconds, test if the time-difference is smaller then the max-value for this unit (60s) and if so, print out "$timeDifference $unit". If not you divide the difference by the units max-value and start over with the next higher unit (minutes).
Example:
$timeDif = 60*60*5 + 45; // == 5 hours 45 seconds
// 60 seconds in a minute
if ($timeDif < 60) // false
return "$timeDif second(s) ago";
// convert seconds to minutes
$timeDif = floor($timeDif / 60); // == 300 = 5 * 60
// 60 minutes in an hour
if ($timeDif < 60) // false
return "$timeDif minute(s) ago";
// convert minutes to hours
$timeDif = floor($timeDif / 60); // == 5
// 24 hours in a day
if ($timeDif < 24)
return "$timeDif hour(s) ago";
// ...
Here's what a MySQL solution might look like:
SELECT date_field, IF (DATEDIFF( NOW(), date_field) < 1, IF ( TIMEDIFF( NOW(), date_field ) < '01:00:00', CONCAT(MINUTE(TIMEDIFF(NOW(), date_field)), ' minutes'), CONCAT(HOUR(TIMEDIFF(NOW(), date_field )), ' hours')), date_field) AS occurred
FROM table
That is no so hard to code. Moreover is quite fun make it. Here a sample, Just put hands to work.
Start converting SQL Datestamp to unixtime like this:
$unixtime = strtotime('2012-02-20 21:14:54');

Comparing timestamps in PHP

How would I be able to return a boolean expression of true once exactly 24 hours have passed and no more?
I am using strtotime() to out put these two times:
$time1 = 1316268919;
$time2 = 1316268898;
This should do what you need:
if (($time1-$time2) == 86400) {
// Exactly 24 hours have passed.
}
strtotime() returns the number of seconds since the Unix epoch. So to find the difference in times, simply subtract one from another. Then compare that difference against the number of seconds in 24 hours (24 * 60 * 60).
abs($time1 - $time2) === 60 * 60 * 24
the abs is just in case; time2 might be bigger than time1.
If the difference in times equals 60 (seconds in a minute) * 60 * (minutes per hour) * 24 (hours for a day) = 86400, there's a day difference
24h=86400sec so, when difference of two is 86400, it's exactly 24h
n.b. twice a year could cause issue do to DST, if applicable.

timestamp countdown in javascript and php

I need some help to code a script that counts down the days, hours, minutes and seconds from a unix timestamp.
timestamp is created with php
<? php echo hours ();?>
And I want the script to count down in real time with JavaScript.
Example
2 days, 3:15:39
Hope someone can help me a little:)
First you have some PHP errors. It should be e.g.
<?php echo time(); ?>
I'm using a timestamp in JavaScript for the ease of showing an example.
http://jsfiddle.net/pimvdb/AvXd3/
// the difference timestamp
var timestamp = (Date.now() + 1000 * 60 * 60 * 24 * 3) - Date.now();
timestamp /= 1000; // from ms to seconds
function component(x, v) {
return Math.floor(x / v);
}
var $div = $('div');
setInterval(function() { // execute code each second
timestamp--; // decrement timestamp with one second each second
var days = component(timestamp, 24 * 60 * 60), // calculate days from timestamp
hours = component(timestamp, 60 * 60) % 24, // hours
minutes = component(timestamp, 60) % 60, // minutes
seconds = component(timestamp, 1) % 60; // seconds
$div.html(days + " days, " + hours + ":" + minutes + ":" + seconds); // display
}, 1000); // interval each second = 1000 ms

Categories