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);
Related
Say i have a variable like $speed = 5.5, what formula would I use to convert that into minutes, so in this case it would be 5 and a half minutes.
I need it to work in this:
date("Y-m-d H:i:s", strtotime("$now - $speed mins"));
Other examples, 2.25 would convert to 2 mins 15 secs, 7:75 to 7 mins 45 secs, etc
Anyone have any ideas? Never been a maths buff.
Just do it with second.
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', $speed * 60)));
If you want more precision, then
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', round($speed * 60))));
You could also use PHP's own DateInterval class (requires PHP 5.3) http://www.php.net/manual/en/dateinterval.createfromdatestring.php
With sample:
$interval = DateInterval::createFromDateString('5.5 minutes');
echo $interval->format('%Y-%m-%d %H:%i:%s')
Could also a Unix Timestamp for dates and 3600 = 1 hour.
For example, the current time would be: $timestamp = gmmktime();
So if you wanted to add ".5" (30 minutes) to the current time, you would say $timestamp + 1800. ".25" would be $timestamp + 900.
$minutes = floor($speed);
$seconds = ($speed - $minutes) * 60;
I am trying to make a schedule from the current hour
to 12 hours afterwards. I am using the date() function
to retrieve the current time but how can I increment the
hours and adjust the AM/PM? Can I just add 1 to the date()
function?
Thank you for your help.
$now = time();
$then = $now + 12 * 60 * 60;
echo date(format, $then);
You can use strtotime:
date('d-m-Y H:i:s', strtotime('+1 hour')); // one hour since now
or
date('d-m-Y H:i:s', strtotime('2011-02-25 14:00:42'));
$mydate = date("dateformat", time() + 43200);
time() gets the current timestamp, then you add 43200 which is 12 hours * 60 mins * 60 secs
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.
Would it be possible to get the unix timestamp 7 days from now?
Would be awesome!
Yes, get the unix timestamp and add 25200 to it. If you want to format that timestamp you can use date().
$future = time() + (60 * 60 * 24 * 7);
date("o", future);
And from the PHP docs for time()
date('Y-m-d', strtotime('+1 week'))
Sure.
Get the current timestamp. Add 7 days worth of seconds.
Note: The timestamp "7 days ahead" (in terms of 7 * 86400 seconds) of the current timestamp may not represent the same day-of-week or the same hour in the day (yay daylight savings!) or even the same second (rare, yay leap-seconds!).
time() + (60 * 60 * 24 * 7); // "good enough"
strtotime('+7 days'); // daylight savings save
Just add seven days?
$future = time() + 60*60*24*7;
// seconds ---^ ^ ^ ^
// minutes ---^ ^ ^
// hours ---^ ^
// days ---^
See time().... oh, the example given there does exactly what you want... I guess you have not read the manual before.
The proper way of doing this on a recent version of PHP is using the DateTime object (in my opinion).
$date = new DateTime('now'); // can be anyting else too
$date->modify('+1 week');
// PHP 5.3
$future = $date->getTimeStamp();
// PHP 5.2
$future = $date->format('U');
$now = time() + (7 * 24 * 60 * 60);
Get the current unix timestamp using time() and then multiply 60 seconds, 60 minutes, 24 hours, and 7 days.
How to get duration in terms of minutes by subtracting a previous time stamp from the present time in PHP?
The format of time stamp is like
2009-12-05 10:35:28
I want to calculate how many minutes have passed.
How to do it?
To do this in MySQL use the TIMESTAMPDIFF function:
SELECT TIMESTAMPDIFF(MINUTE, date_lastaccess, NOW()) FROM session;
Where session is the table and date_lastaccess is a DATE / DATETIME field.
If you don't wanna add a library, you can do this pretty easily:
<?php
$date1 = "2009-12-05 10:35:28";
$date2 = "2009-12-07 11:58:12";
$diff = strtotime($date2) - strtotime($date1);
$minutes_passed = $diff/60;
echo $minutes_passed." minutes have passed between ".$date1." and ".$date2;
?>
Check out some pretty date libraries in PHP. Might have something for you. Here's one : http://www.zachleat.com/web/2008/02/10/php-pretty-date/
strtotime("now") - strtotime("2009-12-05 10:35:28")
That will get you seconds difference. Divide by 60 for minutes elapsed. If you have any information on the time zone of the initial timestamp, however, you'd need to adjust the "now" to a more comparable time
something like that
$second = strtotime('now') - strtotime($your_date);
$minute = $second / 60;
strtotime return the number of seconds since January 1 1970 00:00:00 UTC, so you can easily manipulate this. if you need don't want to have 35.5 minute you can use floor to round up the number. Last remark both time need to be in the same timezone or you are going to count the timezone difference also.
You could just use
$timestamp = time();
$diff = time() - $timestamp
$minutes = date( i, $diff );
function timeAgoMin($timestamp){
if(!is_int($timestamp)){
$timestamp = strtotime($timestamp);
}
return ((time() - $timestamp) / 60);
}