I have a php script that collects data with the current timestamp.
The timestamp in the mysql database is correct (giving the correct time and date), although on the front end it displays : in 5 hours
The script that generates this output looks like this:
function plural($num) {
if ($num != 1)
return "s";
}
function relative_date($date) {
$diff = time() - strtotime($date);
if ($diff>0) {
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return date("M j, Y", strtotime($date));
} else {
if ($diff>-120)
return "Right now";
if ($diff>-60)
return "in " . -$diff . " second" . plural($diff);
$diff = round($diff/60);
if ($diff>-60)
return "in " . -$diff . " minute" . plural($diff);
$diff = round($diff/60);
if ($diff>-24)
return "in " . -$diff . " hour" . plural($diff);
$diff = round($diff/24);
if ($diff>-7)
return "in " . -$diff . " day" . plural($diff);
$diff = round($diff/7);
if ($diff>-4)
return "in " . -$diff . " week" . plural($diff);
return date("M j, Y", strtotime($date));
}
}
How can I get that to show me the correct time?
I have no idea what the problem is, maybe timezone settings. Check them in your PHP.ini and in your code
Related
Hi I'm making an online coupon system for mobile devices. The coupons are for a limited time active and I have to do a check on the coupons. I use date(d/m/Y H:i:s) for just showing the date and time, and I also have a expire date which is a just a string that I later convert into a date.
This is how I do the check if the coupon is expired:
if ($date1B > $date2B) {
echo "<script>alert('Expired coupon!!');</script>";
}
Now what I want is to calculate the days when the coupon will be expired.
This is what I found on W3Schools, but the example below uses date_create(), so you make an custom date and time. I already have 2 dates and times.
$date1 = date("d-m-Y H:i:s");
$date2 = date_format($date2A, 'd-m-Y H:i:s');
$diff = date_diff($date1,$date2);
echo $diff->format("%a days %h Hours %i Minute %s Seconds ");
When I replace variables with the existing variables I get these errors:
date_diff() expects parameter 1 to be DateTimeInterface, string given in
and
Call to a member function format() on boolean in
The full .PHP page:
<?php
date_default_timezone_set("America/Curacao");
$date1A = date("d/m/Y H:i:s");
$date1B = date("dmYHis");
$date2B = "27032017042100";
$date2A = date_create_from_format('dmYHis', $date2B);
echo "Datum 1: " . $date1A . "<br>" ;
echo "Datum 1: " . $date1B . "<br><br>";
echo "Datum 2: " . date_format($date2A, 'd/m/Y H:i:s') . "<br>";
echo "Datum 2: " . $date2B . "<br>";
if ($date1B > $date2B) {
echo "<script>alert('Klaar!!');</script>";
}
$date1 = date("d/m/Y H:i:s");
$date2 = date_format($date2A, 'd/m/Y H:i:s');
$diff = date_diff($date1,$date2);
echo $diff->format("%a days %h Hours %i Minute %s Seconds ");
?>
Updated .PHP file
I fixed the errors.. This is the updated .PHP page:
<?php
date_default_timezone_set("America/Curacao");
$date1A = date("d-m-Y H:i:s");
$date1 = date_create($date1A);
echo date_format($date1,"d-m-Y H:i:s");
echo "<br>";
$date2B = "31032017042100";
$date2A = date_create_from_format('dmYHis', $date2B);
$final = date_format($date2A, 'd-m-Y H:i:s');
$date2 = date_create($final);
echo date_format($date2,"d-m-Y H:i:s");
echo "<br>";
$diff = date_diff($date1, $date2);
echo $diff->format("%R %a days %h Hours %i Minute %s Seconds");
if ($date1 > $date2) {
echo "<script>alert('Coupon Expired!!');</script>";
}
?>
The simplest way to calculate the difference between two dates is date_diff() function which gives error on invalid dates.
Before passing on the values to date_diff function you should use the date_create() function.
Usage:
<?php
$startDate = date_create('2014-06-13');
$endDate = date_create('2017-08-10');
$diff = date_diff($startDate, $endDate);
var_export($diff);
Which gives you output like:
DateInterval::__set_state(array( 'y' => 3, 'm' => 1, 'd' => 28, 'h' => 0, 'i' => 0, 's' => 0, 'weekday' => 0, 'weekday_behavior' => 0, 'first_last_day_of' => 0, 'invert' => 0, 'days' => 1154, 'special_type' => 0, 'special_amount' => 0, 'have_weekday_relative' => 0, 'have_special_relative' => 0, ))
UPDATE
You can use or modify the following function to get the difference:
function dateDifference($startDate, $endDate)
{
try {
$startDate = date_create($startDate);
$endDate = date_create($endDate);
$diff = date_diff($startDate, $endDate);
$d = "";
if ($diff->y != 0) {
if ($diff->y > 1) {
$d .= $diff->y . " Years ";
} else {
$d .= $diff->y . " Year ";
}
}
if ($diff->m != 0) {
if ($diff->m > 1) {
$d .= $diff->m . " Months ";
} else {
$d .= $diff->m . " Month ";
}
}
if ($diff->d != 0) {
if ($diff->d > 1) {
$d .= $diff->d . " Days ";
} else {
$d .= $diff->d . " Day ";
}
}
if ($diff->h != 0) {
if ($diff->h > 1) {
$d .= $diff->h . " Hours ";
} else {
$d .= $diff->h . " Hour ";
}
}
if ($diff->i != 0) {
if ($diff->i > 1) {
$d .= $diff->i . " Minutes ";
} else {
$d .= $diff->i . " Minute ";
}
}
if ($diff->s != 0) {
if ($diff->s > 1) {
$d .= $diff->s . " Seconds ";
} else {
$d .= $diff->s . " Second ";
}
}
return $d;
}
catch(Exception $e)
{
die("ERROR");
}
}
Running it like so:
echo dateDifference('2014-10-16', '2017-06-08');
shall output:
2 Years 7 Months 23 Days
Based on #ShaktiPhartiyal answer, to output in the format that you want, just use:
$startDate = date_create('2014-06-13');
$endDate = date_create('2017-08-10');
$diff = date_diff($startDate, $endDate);
echo $diff->format("%a days %h Hours %i Minute %s Seconds ");
And the output will be:
1154 days 0 Hours 0 Minute 0 Seconds
I am trying to get difference between dates using this code, but it can not produce the right result, what is wrong with this code, I could not find out.
$birth_date = new DateTime("1977-03-23");
$current_date = new DateTime();
$diff = $birth_date->diff($current_date);
echo $years = $diff->y . " years " . $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
echo $months = ($diff->y * 12) + $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
echo $weeks = floor($diff->days/7) . " weeks " . $diff->d%7 . " day(s)"; echo "<br/>";
echo $days = $diff->days . " days"; echo "<br/>";
It shows Outout as - 39 years 10 months 16 days
But I think the output should be - 39 years 10 months 7 days
It calculate year and month correctly but days are wrong.
maybe this function can help you:
function leap_year($date1, $date2)
{
$y1 = $date1->format('Y');
if ($date1->format('m')>2) $y1++;
$y2 = $date2->format('Y');
if ($date2->format('m')<=2) $y2--;
$leap_years = 0;
for($i = $y1; $i <= $y2; $i++)
{
//echo date("Y", strtotime($i . '-01-01')).' '.date("L", strtotime($i . '-01-01')).'<br/>';
if (date("L", strtotime($i . '-01-01')) == 1)
$leap_years++;
}
return $leap_years;
}
echo leap_year($birth_date, $current_date);
Maybe you missed the leap years, this could explain the difference of 9 days, e.g.
9 * 4 y = 36 years
I know this question has been asked several times and I found so many tutorials, blog posts about converting timestamp to ago time in php..
I have tried countless codes and nothing seems to work for me...
I either get a blank page with no errors (i have error rerposting on my php page), or I get some strange numbers in my page..
so I thought someone here could shed a light on this for me..
Basically I am saving date like so:
$date = date('Y-m-d H:i:s');
I simply save it in mysql database...
and I echo it like so:
echo $date;
so now what I need to know is how I can convert the echo $date; to something like 1 minutes ago, 10 minutes ago, 1 hour ago etc etc every time the page closes and reopens?
I did try so many functions that I found on google and noon seem to do anything!
could someone please advise on this issue?
Thanks
EDIT:
I used this code as stated in the answer but I still get the $date echo-ed exactly the same way as its stored in the database which is this format: 2014-10-06 22:54:54
$date = date('Y-m-d H:i:s');
$time1 = new DateTime($date);
$now = new DateTime();
$interval = $time1->diff($now);
if ($interval->y) $date = $interval->y . ' years';
elseif ($interval->m) $date = $interval->m . ' months';
elseif ($interval->d) $date = $interval->d . ' days';
elseif ($interval->h) $date = $interval->h . ' hours';
elseif ($interval->i) $date = $interval->i . ' minutes';
echo $date;
You should use the DateTime class to get the difference between 2 times, ie;
$time1 = new DateTime('2014-10-06 09:00:59');
$now = new DateTime();
$interval = $time1->diff($now,true);
and then use that difference (which is a DateInterval object, $interval) to find the smallest time difference like this;
if ($interval->y) echo $interval->y . ' years';
elseif ($interval->m) echo $interval->m . ' months';
elseif ($interval->d) echo $interval->d . ' days';
elseif ($interval->h) echo $interval->h . ' hours';
elseif ($interval->i) echo $interval->i . ' minutes';
else echo "less than 1 minute";
which should echo (at time of writing) 13 hours.
Hope this helps.
Check this function intval() - http://php.net/manual/en/function.intval.php
The following code should help you out
$seconds_ago = (time() - strtotime('2014-01-06 15:25:08'));
if ($seconds_ago >= 31536000) {
echo "Seen " . intval($seconds_ago / 31536000) . " years ago";
} elseif ($seconds_ago >= 2419200) {
echo "Seen " . intval($seconds_ago / 2419200) . " months ago";
} elseif ($seconds_ago >= 86400) {
echo "Seen " . intval($seconds_ago / 86400) . " days ago";
} elseif ($seconds_ago >= 3600) {
echo "Seen " . intval($seconds_ago / 3600) . " hours ago";
} elseif ($seconds_ago >= 60) {
echo "Seen " . intval($seconds_ago / 60) . " minutes ago";
} else {
echo "Seen less than a minute ago";
}
I'm creating a countdown for how long people can do specific functions on a specific page. I've made a countdown, but he is going under 0, so he's counting further negatively.
I'm printing out the countdown like this:
-4 days -10 h -3 m -34 s remaining to run 1210 meter
My countdown,
$Date = "" . $groups['group_date'] . "";
$stopDate = date('Y-m-d H:i:s', strtotime($Date. ' +' . $periodOfRunning . 'days'));
<h3><?php $rem = strtotime($stopDate) - time();
$day = floor($rem / 86400);
$hr = floor(($rem % 86400) / 3600);
$min = floor(($rem % 3600) / 60);
$sec = ($rem % 60);
if($day) echo "$day days ";
if($hr) echo "$hr h ";
if($min) echo "$min m ";
if($sec) echo "$sec s";
echo " remaining to run " . $totalDistanceToDo . " meter";
?></h3>
If you use DateTime() you can compare the two times and see if the stop time is in the past. If it is, display a different message. Also, this makes getting the interval between the two easier to get and display:
$now = new DateTime();
$stopdate = new DateTime($stopDate);
if ($stopdate > $now) {
$diff = $now->diff($stopTime);
echo $diff->format('%d days, %h hours, %i minutes, %s seconds');
echo " remaining to run " . $totalDistanceToDo . " meter";
}
else {
// negative time. display something else
}
Keep in mind if you don't want to display zero values for time units it will be a little more complex:
$now = new DateTime();
$stopdate = new DateTime($stopDate);
if ($stopdate > $now) {
$diff = $now->diff($stopTime);
if ($diff->d > 0) echo $diff->d . ' days, ';
if ($diff->h > 0) echo $diff->h . ' hours, ';
if ($diff->i > 0) echo $diff->i . ' minutes, ';
if ($diff->s > 0) echo $diff->s . ' seconds';
echo " remaining to run " . $totalDistanceToDo . " meter";
}
else {
// negative time. display something else
}
I have used many functions for relative time in php but that give different results...help me
My functions :
<?php
function pretty_relative_time($time) {
if ($time !== intval($time)) { $time = strtotime($time); }
$d = time() - $time;
if ($time < strtotime(date('Y-m-d 00:00:00')) - 60*60*24*3) {
$format = 'F j';
if (date('Y') !== date('Y', $time)) {
$format .= ", Y";
}
return date($format, $time);
}
if ($d >= 60*60*24) {
$day = 'Yesterday';
if (date('l', time() - 60*60*24) !== date('l', $time)) { $day = date('l', $time); }
return $day . " at " . date('g:ia', $time);
}
if ($d >= 60*60*2) { return intval($d / (60*60)) . " hours ago"; }
if ($d >= 60*60) { return "about an hour ago"; }
if ($d >= 60*2) { return intval($d / 60) . " minutes ago"; }
if ($d >= 60) { return "about a minute ago"; }
if ($d >= 2) { return intval($d) . " seconds ago"; }
else {return "Just Now"; }
}
function plural($num) {
if ($num != 1)
return "s";
}
function getRelativeTime($date) {
$diff = time() - strtotime($date);
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}
echo pretty_relative_time('2012-08-06 8:04:15') ;echo "<br/>";
echo getRelativeTime('2012-08-06 8:04:15');
?>
OutPut :
Just Now // for first function
-15747 seconds ago // for second function
any settings in db ?.....i have used DATETIME for date...
You haven't made clear what output you're expecting, but from your later comment it should be "1 hour ago"? In which case I suspect you are suffering from timezone problems - most likely the dreaded "Daylight Savings Time". Try echoing date('Y-m-d H:i:s') in PHP and seeing if it comes out with the value you expect.
The discrepancy could be in the timezone set in php.ini, or the system clock of the webserver itself, not matching the timezone setting of the database, or the system clock on that server.