so I have a table which lists chat messages in my game server (chat messages are stored in a database) and I have a UNIX timestap like this (for example) 1455749769
Does anyone know how I can use php to convert the timestamp so it echos how long ago the chat message was for example: "5 Seconds Ago"
Here is my table
$name=$row['client_name'];
$time=$row['msg_time'];
$name=htmlentities($name);
echo "<tr>";
echo "<td> $time </td>";
echo "<td><a href='http://144.76.158.173/ech/user.php?id=".$row["client_id"]."' > $name </a></td>";
echo "<td> $msg </td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
echo "</div>";
}
$conn->close();
?>
Any help greatly appreciated :)
The timestamp is seconds since the Epoch, so just get the current timestamp and subtract:
$seconds = time() - $time
Just get the current time and substract:
$now = time();
//results into an unix like 1455750460
//then just substract:
$diff = $now - $time
// gives you the passed seconds
//readable
echo date('H:i:s', $diff);
Related
I want to compare two dates; a receipt date, and a deployment date which has 14 days added to it.
If the receipt date is more than the deployment date, then the data should be highlighted red otherwise it should be highlighted black, and I managed to do this comparison but it only compares days and not years or months. Do I need to make these two dates into strings to compare them?
while ($stmt->fetch()){
if ($date < $deployment_date){
echo '<font color="red">';
}
else{
echo '<font color="black">';
}
echo "<table border='1' style='width:50%'>";
echo "<td>";
echo "<b>Receipt ID: <a href ='transactiondetail.php?receipt=$receipt'>$receipt</b></a>";
echo "<br><br>";
echo "Used By: $officer_id";
echo "<br><br>";
echo "Cost: $cost";
echo "<br><br>";
echo "Area travelled: $area";
echo "<br><br>";
echo "Date of Submission: $date ";
$deployment_date = date_create("$deployment_date");
date_add($deployment_date, date_interval_create_from_date_string('14 days'));
echo date_format($deployment_date, 'Y-m-d');
echo "<br><br>";
echo "<br><br>";
echo "</td>";
echo '</font>';
echo "</table>";
echo "<br>";
}
The DateTime class lets you use the standard comparison operators.
For example,
$receiptDate = DateTime::createFromFormat('Y-m-d', '2000-01-01');
$deploymentDate = DateTime::createFromFormat('Y-m-d', '2001-01-01');
if ($receiptDate < $deploymentDate) {
// do stuff here
}
For date compare please try to use strtotime().
Here is an example:
$date = strtotime($date) & $deployment_date=strtotime($deployment_date)
And then try to compare.
<?php
echo "<table class=\"table table-hover table-bordered\">";
echo "<thead>";
echo "<tr>";
$result=mysqli_query($con,"SELECT day FROM time_slot ORDER BY day;");
while($row=mysqli_fetch_array($result))
{
$days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$day = $row['day']-1;
$day = $days[$day];
echo '<th>';
echo $day;
echo '</th>';
}
echo "</tr>";
echo "</thead>";
require('../includes/connection.php');
$result=mysqli_query($con,"SELECT * FROM time_slot ORDER BY day DESC;");
while($row=mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
This portion of data should be display vertically in table with respect to Day
//This code to split time into intervals
$starttime = $row['start_time']; // your start time
$endtime = $row['end_time']; // End time
$duration = $row['slot_time'];
$array_of_time = array ();
$start_time = strtotime ($starttime); //change to strtotime
$end_time = strtotime ($endtime); //change to strtotime
$add_mins = $duration * 60;
while ($start_time <= $end_time) // loop between time
{
echo '<td>';
echo "<div align=\"center\">";
print_r(date ("h:i", $start_time) ."-". date("h:i", $start_time += $add_mins));
//
echo "</br><input type=\"radio\" name=\"texi\" value=\"1\" ></input>";
echo '</td>' ;
echo '</div>';
}
echo "</tr>";
echo "</tbody>";
}
mysqli_close($con);
echo "</table>
</div>
<input type=\"submit\" value=\"Book Slot\" class=\"button_drop\">
</form>";
?>
I want to show time slots vertically with respect to day in table.
This is the output of this code:
But I want to display time which is showing horizontally that should be shown vertically. Eg. 1-3, 3-5, 5-7 of first row should be shown below Monday, then next row should be shown below Tuesday and so on.
Try using a query like this:
SELECT day FROM time_slot ORDER BY time_slot, day;
That should get the data in an easier order to process. I can't see your exact data structure but it looks like you should get 7x 01:00-03:00, then 7x 03:00-05:00 etc. Each set should be in order by day. Then you can just write out your <td>s in the order the data comes from the DB. Keep track of when the day value changes (or just count up to 7) and start a new <tr> at that time.
I am trying to find out how to make this script work?
Basically if the difference between the date stored into the db and today's date is greater then 10 days then the links should be in red if not they should be in black.
<?php
// Today's date
$today = date("d/m/Y");
// The date stored into the DB
$NewsDate = date("d/m/Y", strtotime($getInfo['date']));
// The date stored into the DB + 10 days
$NewsDatePlus10 = date("d/m/Y", strtotime($NewsDate) + (86400 * 10));
if ($NewsDate <= $NewsDatePlus10) {
echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>";
echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])." <small>". date("d/m/Y", $getInfo['date'])."</small></span>";
} else {
echo "<span style='color:red;'>";
echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>";
echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])." <small>". date("d/m/Y", $getInfo['date'])."</small></span>";
echo "</span>";
}
?>
in the database the date is stored into an int(11) and it look like this "1465929874" now as you can imagine I did not design the first part I just try to make it do what I need it to do.
I'm unsure as to why you are converting them to d/m/Y format to store in a variable, when you aren't using that variable in any other place than to check.
You should just be able to do:
//Date stored in database
$NewsDate = strtotime($getInfo['date']);
// The date stored into the DB + 10 days
$NewsDatePlus10 = strtotime($NewsDate) + (86400 * 10);
//Now you have a timestamp here.
if ($NewsDate < $NewsDatePlus10) {
//Code here
} else {
Don't do maths with text and you won't need to reinvent the wheel:
$today = new DateTime();
$NewsDate = new DateTimeImmutable($getInfo['date']);
$NewsDatePlus10 = $NewsDate->modify('+10 days');
if ($NewsDate <= $NewsDatePlus10) {
} else {
}
Make sure that $getInfo['date'] comes in an unambiguous format.
Here is your full answer:
<?php
$today = new DateTime();
$NewsDate = date("d/m/Y", strtotime($getInfo['date']));
$NewsDatePlus10 = $NewsDate->modify('+10 days');
if ($NewsDate <= $NewsDatePlus10) {
echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>";
echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])." <small>". date("d/m/Y", $getInfo['date'])."</small></span>";
} else {
echo "<span style='color:red;'>";
echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>";
echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])." <small>". date("d/m/Y", $getInfo['date'])."</small></span>";
echo "</span>";
}
?>
But please check your date format when you are retrieving.
When you want to add dates,
For e.g. $NewDate=Date('Y-m-d', strtotime("+3 days"));
For decrease the dates, For e.g. $NewDate=Date('Y-m-d', strtotime("-3 days"));
You can use this example as per your need. this will give you the number of days old you db date is.
$now = time(); // current time
$your_date = strtotime("15-09-2016"); // db date
$datediff = $now - $your_date;
$no_of_days = floor($datediff / (60 * 60 * 24));
if($no_of_days > 10){ echo 'red';}
else { echo 'black';}
My server time is GMT, so I convert it to Asia/Kuala Lumpur timing.I need to calculate time difference between current time and the time request was made. The time request was made is stored in database and retrieved in $reset_req variable.
$reset_req="2015-06-30 11:30:23";
$timezone_offset = +8; // us central time (gmt-6) for me
if(isset($reset_req)){
$request_date2 = strtotime($reset_req)+$timezone_offset*60*60;
}
echo "current time= ".strtotime(time());
echo"<br/>";
echo "time req made=".strtotime($request_date2);
echo"<br/>";
$timediff = strtotime(time()) - strtotime($request_date2); // in seconds
if($timediff < 2 hours)//how to calculate 2 hours here
{
//do something
}
Everyone already said how to calculate 2 hours (2* 3600sec). But what they didn't tell you is that you can't do this: strtotime(time()); time(); already gives you a Unix timestamp so you can't convert it twice.
Your Code should look more like this
<?php
$reset_req = "2015-06-30 11:30:23";
$timezone_offset = +8; // us central time (gmt-6) for me
if(isset($reset_req)){
$request_date2 = strtotime($reset_req)+$timezone_offset*60*60;
$current = time();
} else {
echo '$reset_req was not set';
exit;
}
echo "current time= " . $current . "<br />";
echo "time req made=" . $request_date2 . "<br />";
$timediff = $current - $request_date2; // in seconds
if($timediff < (2*3600))//how to calculate 2 hours here
{
echo "less than 7200 sec have past since $request_date2. Past: $timediff seconds";
}else{
echo "more than 7200 sec have past since $request_date2. Past: $timediff seconds";
}
?>
$hour = 3600; //an hour has 3600 seconds
if($timediff < 2 * $hour )
{
//do something
}
$timediff is in seconds. So you have to convert that to hours or convert the 2 hours to seconds and then need to compare like as follows:
replace
if($timediff < 2 hours)//how to calculate 2 hours here
{
//do something
}
with
if($timediff < (2*3600))//how to calculate 2 hours here
{
//do something
}
Try this
<?php
$reset_req = "2015-06-30 11:30:23";
//set default timezone here
date_default_timezone_set("Asia/Kuala_Lumpur");
$request_date2 = '';
if(isset($reset_req))
{
$request_date2 = strtotime($reset_req);
}
echo "Current time = ".time();
echo"<br/>";
echo "Time req made =".$request_date2;
echo"<br/>";
$timediff = time() - $request_date2; // in seconds
echo "<br>Time Differnce : ".$timediff;
//how to calculate 2 hours here
if($timediff < 2*3600)
{
//do something
}
?>
I am comparing a deadline time from my database which is on a 24-hour format (like 3:00 = 15:00) to the time now.
For example, if a student wasn't able to submit on the due time, an upload link will be greyed out.
I'm okay with the date comparison, my problem is the time.
Here's my current code:
$date = date("Y-m-d");
$time = strtotime(date('G:i:s'));
$deadtime = strtotime($r['upload_deadtime']);
/*------$deadtime = 15:00:00 ---------*/
if(date($r['upload_deadline']) >= $date && $deadtime > $time){
echo '<td>upload</td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
Update: let's just forget about the date comparison, what I'm trying to say is how can I compare my deadline time (which is on a 24-hour format (like 3:00:00 = 15:00:00)) to the time now.
Here's my current code:
$time = strtotime(date('G:i:s')); /*which returns 23:15:42 instead of 14:15:42 */
$deadtime = strtotime('15:00:00');
if($deadtime > $time){
echo '<td>upload</td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
date_default_timezone_set('Asia/Manila');
$deadtime = strtotime("15:00:00");
if ($deadtime - time() > 0){
echo '<td>upload</td>';
}else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
This may solve your time comparison issue!!
strtotime("15:00:00") will convert time into unix timestamp of today's 15:00:00 and time() have current unix timestamp.
You just have the comparison backwards on your time check.
You are checking if the deadline date is in the future (assuming the script always runs in the present) and then you are checking that the deadline time is less than the current time (IE before), on a date time epoch value.
With the check you are doing on the strtotime, you do not need to check the dates, as they are already included with the date and time by means of epoch.
Simply do:
if($deadtime > $time){
echo '<td>upload</td>';
} else {
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
This will only show the upload link in the table if the deadline time is in the future.
EDIT:
In the case of your new code, you just need to make the deadtime a strtotime of the deadline date and time stamp from your dBase. Then the code will work.
You are still also comparing the wrong way around!
Example:
/* deadtime = strtotime of datetimestamp in format yyyy-mm-dd G:i:s */
/* $time = strtotime of now. */
if($deadtime > $time){
echo '<td>upload</td>';
} else {
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
Use time PHP's DateTime::Diff
// Current Date and Time
$date_time_now = new DateTime();
// Date Time values from DB (Assuming it's coming from database)
$date_time_deadline = new DateTime('2014-09-08 15:00:00');
// Calculate difference
$difference = $date_time_now->diff($date_time_deadline);
// Output difference in human-readable format
echo "Difference: " . $difference->format('%d days, %h hours, %i minutes, %s seconds');
http://php.net/manual/en/datetime.diff.php
Here's my working code. I think you'll know that my point is.
date_default_timezone_set('Asia/Manila');
$time = date('H:i:s');
$deadline_time = date("h:i a",strtotime($r['upload_deadtime']));
list($hours,$mins,$secs) = explode(":",$time);
$hours = date("H") + 9;/*-gives me wrong hours so i got to modify it-*/
$time_array = array($hours,$mins,$secs);
$new_time =implode(":",$time_array);
$deadtime = strtotime($deadline_time);
$time_now = strtotime($new_time);
if($r['upload_deadline'] < $date && $deadtime < $time_now){/*-compares the date and time-*/
echo '<td>upload</td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}