This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting timestamp to time ago in php e.g 1 day ago, 2 days ago
I'm trying to display how old the post is through php. What I'm doing is storing in mysql the php timestamp using the code below.
$timestamp=time();
So then I'm left with a number like this
1334216865
How would I go about changing it so it will say example:
8 seconds ago
If you refresh the page 10 seconds later it will show:
18 seconds ago
I'm not sure how I would convert a timestamp back into time to show my examples.
Thank you
Or you could make a little "time_ago" function:
function ago($time) {
$timediff=time()-$time;
$days=intval($timediff/86400);
$remain=$timediff%86400;
$hours=intval($remain/3600);
$remain=$remain%3600;
$mins=intval($remain/60);
$secs=$remain%60;
if ($secs>=0) $timestring = "0m".$secs."s";
if ($mins>0) $timestring = $mins."m".$secs."s";
if ($hours>0) $timestring = $hours."u".$mins."m";
if ($days>0) $timestring = $days."d".$hours."u";
return $timestring;
}
echo ago(1334214962);
echo (time() - $post_timestamp) . " seconds ago";
very simple.
Just run the time() function again and subtract the posted time - then do some arithmetic to output how long it has been: 60 seconds per minute, 60 minutes per hour,etc... the time function is how many seconds since some event a long time ago. so, every increment is 1 second on the time function.
Related
This question already has an answer here:
What is a Unix timestamp and why use it?
(1 answer)
Closed 4 years ago.
I want to find 15 minutes different between link has sent time and link activated time.For that, I am as below,
<?php
$sentTime=strtotime(date("2018-05-29 06:11:42"));
$curTime=strtotime(date("Y-m-d h:i:s"));
$difference=$curTime-$sentTime;
echo $sentTime;
echo '<br>';
echo $curTime;
echo '<br>';
echo $difference;
?>
When I do like this it gives some number,
What is those number indicate?
If it is time, whether is it in minute or seconds or milli seconds?
Can anybody assist me?
The function strtotime() returns a timestamp, which is the number of seconds elapsed since Jan 01 1970 (UTC).
$sentTime, $curTime and $difference are seconds.
Please use like this way, its the proper way to declare date
$senttime= date("Y-m-d H:i:s",strtotime("2018-05-29 06:11:42"));
This question already has answers here:
get next and previous day with PHP
(11 answers)
Closed 7 years ago.
I need to figure out
5 minutes before given time
10 seconds before given time
I have try this code
echo $time.'<br />';
echo 'fivemin'.$fivemin= date("H:m:s",strtotime("-5 minutes",strtotime($time)));
echo '<br />tensec'.$tensec=$datetime_from = date("H:m:s",strtotime("-10 seconds",strtotime($time)));
result
15:55:44
fivemin 15:04:44
tensec 15:04:34
You can use the DateTime::sub method for this.
// Set the initial date/time
$date = new DateTime('2015-04-03 12:00:00');
// Go back 5 minutes
$date->sub(new DateInterval('PT5M'));
// $date is now 2015-04-03 11:55:00, go back another 10 seconds...
$date->sub(new DateInterval('PT10S'));
You can also combine the two subs into one (I just split them so you have a better idea of what's going on and you may want to run some actions in between the subs):
$date->sub(new DateInterval('PT5M10S')); // 5 minutes and 10 seconds ago
That will subtract 5 minutes and 10 seconds in 1 go.
You will then end up with $date being a DateTime object, that is now representing 2015-04-03 11:54:50.
This question already has answers here:
How to check if time is between two times in PHP [duplicate]
(2 answers)
Closed 8 years ago.
I'm sure this is a pretty basic task, but I'm quite new to programming and am a bit confused.
Basically I want to execute a different action based on the time of day. For example, if the time is between 05:00 and 20:00 (8pm), run "scriptA". ELSE, or if time is between 20:00 and 05:00, run "scriptB".
What is the easiest way to do this? I basically want to run one script during the "Day" and the other at "Night".
Thank you!
You can try using the date function.
echo date("H:i:s");
will display the current time. Get this string. Compare it and use an if/else.
PHP Date Reference Manual
<?php
if((date("H") > 5) && (date("H")<20) )
{
//run_my_day_script
}
else
{
//run_my_evening_script
}
?>
Get the hour of the day in a 24 hour format, without leading zeros.
$hour = (int)date("G");
Then you can do a less or greater that check.
if ($hour >= 5 && $hour <= 20)
{
// Do Something.
}
http://php.net/manual/en/function.date.php
This question already has answers here:
php string in a date format, add 12 hours
(4 answers)
Closed 8 years ago.
I'd like to take a number of hours imputed in a text box and get a timestamp back so I can create a countdown timer.
The countdown is fine so ignore how that is done etc. But whats the best way to get a timestamp '48' hours from now. For example user has entered 48?
$hours = 48;
$timestamp = (new DateTime())->modify("+{$hours} hours")->format('U');
You can create a timestamp like this:
<?php
strtotime(date('d-m-Y H:i:s') . "+ 48 hours");
?>
Your looking for strtotime. For example, you can just use strtotime('+48 hours'), and it will return a unix timestamp for that time.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert Unix timestamp to hhmmss?
I have a timestamp in the format of:
1330559989
How to check the hours in the timestamp is in between 6 hours and 23 hours?
Can someone help me please?
date('H', 1330559989) will give you the hours in 24h format with a leading zero, in this example: 23.
There is also a specific function for the hour of a timestamp called getdate which offers an array interface to that information (demo):
getdate(1330559989)['hours']; # 23
$hrs=date('H', 1330559989);
if ($hrs>= 6 && $hrs<= 23) {
// your code
}
If you want to check the time between now and that given timestamp, then you can use:
<?php
echo date("H", strtotime(now)-1330559989); // Displays the difference from now.
echo date("H", 1330559989); // Displays that time!
?>
Hope this helps! :)