Subtract fetched datetime from current date time - php

I need to calculate the time difference between the localDatetime and a Datetime fetched from a specific row in my database-table(csv) called ReportedDateAndTime. I searched a lots of solutions but it doesn't working for me. Displaying localtime & the fetched time from Database are working. But the time difference doesn't. Here is my code so far. Please help. Thanks in advance!
<?php
include_once('connection.php');
$sql="SELECT * FROM csv";
$records=mysqli_query($conn, $sql);
$date = new DateTime(null, new DateTimeZone('Asia/Colombo'));
while ($csv=mysqli_fetch_assoc($records))
{
$exp = $csv['ReportedDateAndTime'];
}
$diff = $date->diff($exp);
echo $diff->format('%H')
?>

From the mysql doc
SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
'2004-01-01 13:00:00'
SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
'2004-01-01 22:00:00'
https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

Thanks all. Fortunately I found an answer. :)
<?php
include 'connection.php';
date_default_timezone_set('Asia/Colombo');
$the_time = date('H:i');
echo $the_time." </br>";
$sql="SELECT ReportedDateAndTime FROM csv";
$result=$conn->query($sql);
while ($row = $result->fetch_assoc())
{
$new_time=$row['ReportedDateAndTime'];
echo "<br/>";
echo $new_time." ReportedDateAndTime</br>";
$datetime1 = new DateTime($the_time);
$datetime2 = new DateTime($new_time);
$interval = $datetime1->diff($datetime2);
$hours=$interval->format('%h');
$minutes= $interval->format('%i');
echo "<br/>";
echo $hours." Hours ".$minutes." Minutes</br></br>";
}
?>

Related

round down to nearest 5 mintues php mysqli

I"m trying to get this time code to round down to the nearest time
example if the time listed is 10:28, then i want it to show 10:25
What I have is (the time modify is required)
<?php
include('../../database/2ndconnection.php');
$sql="SELECT * FROM raid ORDER BY TimeRecord DESC LIMIT 1";
$result=mysqli_query($dbcon,$sql);
while($rows=mysqli_fetch_array($result)){
$dateStr = $rows['TimeRecord'];
$pst = new \DateTime($dateStr);
$pst->modify("-3 hours");
$est = new \DateTime($dateStr);
} ?>
PST: <?php echo $pst->format('H:i'); ?>
<br>EST: <?php echo $est->format('H:i'); ?>
thanks in advance for your help
Insert this:
<?php
$currentMinutes = $time->format('i');
$minutesToSubtract = $currentMinutes % 5;
$time = $time->sub(new DateInterval('M' . $minutesToSubtract));
?>
$currentMinutes will hold the current number of minutes, using %, you can compute the minutes that need to be subtracted, and afterwards you subtract them through the sub function
It could be integrated into your code as following:
<?php
include('../../database/2ndconnection.php');
$sql="SELECT * FROM raid ORDER BY TimeRecord DESC LIMIT 1";
$result=mysqli_query($dbcon,$sql);
while($rows=mysqli_fetch_array($result)){
$dateStr = $rows['TimeRecord'];
$pst = new \DateTime($dateStr);
$pst->modify("-3 hours");
$est = new \DateTime($dateStr);
}
$currentMinutes = $pst->format('i');
$minutesToSubtract = $currentMinutes % 5;
$pst = $pst->sub(new DateInterval('M' . $minutesToSubtract));
?>
PST: <?php echo $pst->format('H:i'); ?>
<br>EST: <?php echo $est->format('H:i'); ?>

PHP remove the New text after 1 day

I would add the New text if one day is not passed but if it is passed i would delete the text New if one day is passed.
I tried this code :
<?php
$farkZaman = time() - strtotime("2017-09-26 19:00:00");
$farkGun = floor($farkZaman / (60 * 60 * 24));
if($farkGun < 1){
echo "Yeni";
}
?>
How can i do ?
I need your help.
Note : I have French but i don't have a good English.
Don't know if this is what you are looking for.
Here is set $dt as tomorrow with a time (about 24 hours from now GMT+1).
Then I compare if time is less than this time.
If it is I echo the text.
$dt = strtotime("2017-09-29 06:40");
If(time()<=$dt){
Echo "the text";
}
You can do something like this.
if(strtotime($date_variable) > strtotime("-1 day")) {
echo "New";
}
Try like this
<?php
$now = new DateTime();
$now->format('Y-m-d H:i:s');
$created_date = new DateTime(date('Y-m-d', strtotime("2017-09-27 13:00:00")));
$day = $created_date->diff($now)->days; // 0
if($day < 1){
echo "new texts";
}
?>

Datediff issue - PHP,Mysql

So in my database i've got regdate (date type), but no matter what date it is, the code keeps returning 1.
<?php
$con = mysqli_connect("localhost","root","","login");
if (mysqli_connect_error()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users");
$row = $result->fetch_array();
$date1 = new DateTime($row['regdate']);
$date2 = new DateTime("now");
$interval = $date1->diff($date2);
echo "It has been " .$interval->d." days ";
//$interval is supposed to be difference between regdate and todays date
I hope I'm not missing something stupid. Thank you for your anwers.
SOLVED
I belive this might be more easier.
SELECT DATEDIFF(NOW(),$row['regdate']);
and I think you need a while loop to find all the records.

I have Week and year in php I need to convert date complete

I have Week and year in php I need to convert date complete, I'm new in php help please
<?php
$weekY = date('Wy' ,strtotime('-1 week'));
print_r(date("Y-m-d",strtotime($weekY)));
?>
the result is 1970-01-01
and I have the 5113(Wy) in my database how to convert in Wy
<?php
$weekY = date('Wy' ,strtotime(5113));
echo $weekY."<br/>";
?>
the result is 0170
I read examples but do not help me in my problem
Make use of setISODate
<?php
$gendate = new DateTime();
$gendate->setISODate(2013,52);//year and week
echo $gendate->format('d-m-Y'); //"prints" 23-12-2013
thanks for everything, and I have the answer to my question
<?php
$y = substr('5213',-2);
$Y = 20..$y;
$W = substr('5213',0,2);
echo "Year + Week: ".$Y." ".$W."<br/>";
$date = new DateTime();
$date->setISODate($Y, $W);
echo "Result: ".$date->format('Y-m-d') . "\n";
?>

"Time ago" from MySQL timestamp

I'm trying to make a table of results including the timestamp for each entry from the database. However, I need the time to be displayed as "X seconds/minutes/hours ago".
I've tried the jQuery timeago plugin with zero success and am now wondering how to do this for each entry.
$result = mysql_query("SELECT * from realmfeed order by ID asc");
echo "<table class='display'>\n";
while($row = mysql_fetch_array($result))
{
echo "<tr><td><b>".$row['ID']."</b></td>\n";
echo "<td>".$row['eventType']."</td>\n";
echo "<td>".$row['server']."</td>\n";
echo "<td>".$row['realm']."</td>\n";
echo "<td>".$row['name']."</td>\n";
echo "<td>".$row['time']."</td>\n</tr>\n\n";
}
echo "</table>\n";
How is it possible to create a "time ago" function for each result?
$time = strtotime($row['time']);
$dbDate = new DateTime($time);
$currDate = new DateTime();
$interval = $currDate->diff($dbDate);
echo $interval->d." days ".$interval->h." hours";
please refer to DateInterval for available functions and fields
The problem with using PHP to do the time difference calculations is you're assuming the MySQL and PHP servers are in the same time zones. Better to do the difference calculation in the SELECT:
SELECT TIMEDIFF(NOW(), `time`) AS ago FROM realmfeed ORDER BY ID ASC
This will return hh:mm:ss formatted time difference.
Or if you'd prefer just the number of seconds:
SELECT TIME_TO_SEC(TIMEDIFF(NOW(), `time`)) AS secondsAgo FROM realmfeed ORDER BY ID ASC
I would get the time stamp from MYSQL then do the maths in php eg.
$result = mysql_query("SELECT ..., UNIX_TIMESTAMP(time) as time FROM realfeed ORDER BY ID ASC");
$row = mysql_fetch_array($result);
$then = $row['time'];
$now = time();
$diff = $now - $then; //Now you have the difference in seconds
There is a nice function here you could use the difference on although I have not checked it myself.....
http://itwigle.com/twig/PHP_Time_Ago_Function
try this this may help
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="jquery.timeago.js"></script>
</head>
<body>
<?php
$result = mysql_query("SELECT * from realmfeed order by ID asc");
echo "<table class='display'>\n";
while($row = mysql_fetch_array($result))
{
echo "<tr><td><b>".$row['ID']."</b></td>\n";
echo "<td>".$row['eventType']."</td>\n";
echo "<td>".$row['server']."</td>\n";
echo "<td>".$row['realm']."</td>\n";
echo "<td>".$row['name']."</td>\n";
echo "<td><abbr class=\"timeago\" title=\"".date("j F Y h:i:s A",$row['time'])."\"></abbr></td>\n</tr>\n\n";
}
echo "</table>\n";
?>
<script>
jQuery(document).ready(function($){
$("abbr.timeago").timeago()
});
</script>
</body>
</html>

Categories