PHP date comparison function - php

What is the best way to figure out if timestamp 1263751023 was more than 60 min ago?

$time = 1263751023;
if((time() - $time) > 60 * 60)
{
echo "Yes";
}
There are two basic way to figure this out. You can either figure out what an hour ago was and then check to see if the time you are checking was after that.
(time() - (60*60)) > $time;
The other way is you check what an hour after the time you are checking was, and see if that has passed yet.
($time + (60*60)) < time();
Oh, and the last is to check the difference between the two times, which will get you the number of seconds that have passed
(time() - $time) > (60*60)
All will get you the same answer.

One way is to calculate the difference between the one timestamp and the current timestamp:
$diff = time() - $timestamp;
And then test if that value is greater than 3600 (60 minutes with each 60 seconds):
$timestamp = 1263751023;
$diff = time() - $timestamp;
if ($diff > 3600) {
// timestamp is more than 60 minutes ago
}

$hour = 60*60; // one hour
$time = 1263751023; // zhere you could also use time() for now
if ($time + $hour < time())
{
// one hour a go
}

Related

PHP Simple For loop only loop today's date with time function

I need to write a simple php code that decrements day by day and stop when a certain date hits (in this case yesterday). I have written following code piece to do this but this seems to be looping forever ... not stooping at the forloop break condition i set
//start time to decrements
$time = time(); // for now start with today or set with strtotime('2010-08-01 00:00:00');
for ($time = time(); $time >= ($time - (24 * 3600)); $time = ($time - (24 * 3600))) {
run("www.xyx.com",date("Y/m/d", $time));
}
Any ideas ?
$time >= ($time - (24 * 3600)) is always going to be true. It is the same as saying
n >= n - 1
which is self-evidently true for all n.
Write it like this instead:
$end_time = time() - (24 * 3600); // yesterday
for ($time = time(); $time >= $end_time; $time = ($time - (24 * 3600))) {
run("www.xyx.com",date("Y/m/d", $time));
}
I think the problem is your use of the time() function from within the for loop. Try setting the start and stop variables before the loop.
//start time to decrements
$starttime = time(); // for now start with today or set with strtotime('2010-08-01 00:00:00');
$stoptime = $starttime - (24*3600);
for ($time = $starttime; $time >= $stoptime; $time = ($time - (24 * 3600))) {
//echo date("Y/m/d", $time) . "\n";
run("www.xyx.com",date("Y/m/d", $time));
}

Want to subtract hour to hour, minutes to minutes, and seconds to seconds from between two dates

<?php
$ts='2011-04-13 23:00:00';
$ts1='2011-04-14 15:45:00';
echo $addtime = date("h:i:s", mktime(date("h", $ts1)- date("h", $ts),date("i", $ts1)- date("i", $ts),date("s", $ts1)- date("s", $ts),0,0,0));
?>
It gives a result but it is not correct in many cases. How do I fix it?
Your expected result would be 16:45:00 for the given example, right? So you want the difference between the two given dates in hours:minutes:seconds.
<?php
//initial strings
$ts='2011-04-13 23:00:00';
$ts1='2011-04-14 15:45:00';
//converting to time
$start = strtotime($ts);
$end = strtotime($ts1);
//calculating the difference
$difference = $end - $start;
//calculating hours, minutes and seconds (as floating point values)
$hours = $difference / 3600; //one hour has 3600 seconds
$minutes = ($hours - floor($hours)) * 60;
$seconds = ($minutes - floor($minutes)) * 60;
//formatting hours, minutes and seconds
$final_hours = floor($hours);
$final_minutes = floor($minutes);
$final_seconds = floor($seconds);
//output
echo $final_hours . ":" . $final_minutes . ":" . $final_seconds;
?>
This gives me correct results. Hope I got your problem right!

php simple countdown

I have a date like 3/3/2012 10:56:34 and i'd like to convert it to a countdown (like ebay).
It doesn't have to be dynamic, just something like
3h 2m
2d 4h
3m
etc
It doesn't have to show years, just days, hours, mins, secs, whichever is applicable.
So if there's over a day to go, it'll show days and hours, if under a day, just hours mins, if under an hour, just mins.
Is there a simple way to do this?
UPDATE
This is what I have, but doesn't work (fixed)
$timediff = round(strtotime($rs[ends]) - strtotime($now));
while ($timediff > 86400) { $timediff = $timediff - 86400; $days++; }
while ($timediff > 3600) { $timediff = $timediff - 3600; $hours++; }
while ($timediff > 60) { $timediff = $timediff - 60; $mins++; }
$secs = $timediff;
echo $days . "d " . $hours . "h " .$mins . "m";
Use strtotime to convert your string to the Unix timestamp, then calculate the difference between now and the timestamp and then you'll easily manage to extract days, hours, minutes, etc...
Example for secs and mins from the top of my head (applicable for $timediff < 1 hour, in your case):
$seconds = $timediff % 60; $minutes = floor($timediff / 60);
etc, etc...
first use strtotime to convert to the unix time stamp if necessary.
simply calculate the times you want it to count down by:
1day is 86400 seconds.
1hour is 3600 seconds
Use SQL to get the time stamp then simply if and ifelse statements.
$currenttime=time();
if($currenttime-$somearray[0][time_stamp]<3600)
{
$seconds=$curenttime-$somearray;
echo"$seconds seconds ago";
}
elseif($currenttime-$somearray[0][time_stamp}<86400)
{
$seconds=$currenttime-$somearray;
$minutes=floor($seconds/60);
echo"$minutes minutes ago";
}
and just continue the elseif statements for whatever intervals you would like.

Create Variable in PHP Equal to Current Time Minus One Hour

In PHP, how could I create a variable called $livetime that equals the current time minus 1 hour?
Another way - without all the math and, in my opinion, reads better.
$hour_ago = strtotime('-1 hour');
If you're looking for how to display the time in a human readable format, these examples will help:
$livetime = date('H:i:s', time() - 3600); // 16:00:00
$livetime = date('g:iA ', time() - 3600); // 4:00PM
$livetime = time() - 3600; // 3600 seconds in 1 hour : 60 seconds (1 min) * 60 (minutes in hour)
See time PHP function for more information.
convert your date to strtotime and then subtract one hour from it
$now = date('Y-m-d H:i:s');
$time = strtotime($now);
$time = $time - (60*60); //one hour
$beforeOneHour = date("Y-m-d H:i:s", $time);
You could use the date_create function along with the date_sub function like I have shown here below: -
$currentTime = date_create(now());
$modifyTime = date_sub($currentTime,date_interval_create_from_date_string("1 hour"));
$liveTime = $modifyTime->format('Y-m-d H:i:s');
Assuming that a timestamp is fine you can use the time function like so
<?php
$livetime = time() - 60 * 60;
Current time is equal to time() (current time given in seconds after Unix epoch).
Thus, to calculate what you need, you need to perform calculation: time() - 60*60 (current time in seconds minus 60 minutes times 60 seconds).
$time_you_need = time() - 60*60;
First convert hours into seconds (3600) then use the following:
$your_date = date('F jS, Y',time() - 3600);

How many seconds from mysql time format to now

I use this function to set a time date("Y-m-d H:i:s", strtotime("+2 minutes"). Now I want to compare that value with the current time to find the amount of seconds it's left.
For example compare: $next_action = 2011-01-16 18:03:00 and $now = 2011-01-16 18:01:23. To find the amount of seconds.
strtotime can convert mysql timestamps to unix timestamps. so you just convert both of them to UNIX timestamps and subtract one from other, and you'll get the difference in seconds.
$next_action = "2011-01-16 18:03:00";
$now = "2011-01-16 18:01:23";
echo strtotime($next_action)-strtotime($now);
Why did you convert them to "Y-m-d H:i:s" in the first place? Unix timestamps are much easier to work with.
$start_time = date("Y-m-d H:i:s", strtotime("+2 minutes"));
$time_diff = (time() - strtotime($start_time)); // difference in seconds
$seconds = $time_diff % 60;
$minutes = ($time_diff - $seconds) % (60 * 60);
$hours = ($time_diff - ($minutes * 60) - $seconds) / (24 * 60 * 60);
Untested, but it would probably go something like this.

Categories