In my database I have a bunch of Last Login dates for my users. They are written like this: Last Login: Apr 08 2015, 22:53:49 CEST
And: Last Login: Apr 08 2015, 22:53:49 CET
So both CEST
and CET
at the end can occur.
What I'd like to do is however, is to see how many days ago that was (rounded). Is there any way I can do that with this format? It has to be in this format. I'd like to echo out the following on my website:
Last Login: Apr 08 2015, 22:53:49 CEST (46 days ago)
I can't find any examples for this format. Most of them are in milliseconds or so. Is this even possible?
I only need the days. Hours and minutes are not important.
It's basically this: $date=date('d M Y');
Step 1 :
Remove the Last part of your date ie, CET
or CE
$Date = substr($Date, 0, strpos($Date, " "));
It will the character after last spaces
Step 2
Find the Difference between current date and the extracted date by
$interval = $datetime1->diff($datetime2);
And here's the entire code :
<?php
$Date = "Apr 08 2015, 22:53:49 CES";
$Date = substr($Date, 0, strpos($Date, " CE"));
$now = date('Y-m-d');
$datetime1 = new DateTime($Date);
$datetime2 = new DateTime($now);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
If you want to print the different message if there is 0 days difference then just check if there is 0
day difference and handle it
if ($interval->format('%R%a')=="-0")
{
echo 'Last Login Today';
}
else
{
echo $interval->format('%R%a');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.