Get future timestamps - php

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.

Related

if date time difference is over cetain amount

$now = new DateTime();
$future_date = new DateTime($date);
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Basically I want to use the difference between the two dates now and whatever the person has booked. I need to use an if statement to check if the difference is between 24 hours / 1 day.
how do i use datetime in if statements?
like
If ($interval > 24 hours) {
allow } my problem is how do i write 24 hours in php? sounds really dumb I know.
$now_ts = $now->getTime();
$future_date_ts = $future_date->getTime();
if ($future_date_ts - $now > 60 * 60 * 24) {
// more than 24 hours before $future_date
}
If you also want the diff the other way add:
$now - $future_date_ts > 60 * 60 * 24 // more than 24 hours after $future_date
This gives you a 48 hour range around $future_date.
You could instead use the UNIX timestamp ( the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
time(); will return the current Unix timestamp and you can convert your future_date to a timestamp using strtotime();
So you can calculate the difference in seconds using:
$diff_secs = strtotime($future_date) - time();
And knowing 1 day is 24 hours which is 24*60 minutes which is 24*60*60 seconds,
$diff_days = (strtotime($future_date) - time())/(24*60*60);
Or you could use the difference in hours as your comparison :
$diff_hours = (strtotime($future_date) - time())/(60*60);
if($diff_hours > 24) {
//do something if its more than 24 hours away
}

php convert number into minutes

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;

PHP Compare a difference of timestamp and a real time

I would like to compare a difference between two timestamps and a time in base 60 or 10.
More precisely if timestamp1-timestamp2 is longer (or no) than x hours and y seconds.
I'm using DateTime and DateInterval classes, but there isn't such a function, and i don't find a clean solution.
Thanks
$time1 = new DateTime("2011-01-26 01:13:30"); // string date
$time2 = new DateTime();
$time2->setTimestamp(1327560334); // timestamps, it can be string date too.
$interval = $time2->diff($time1);
echo $interval->format("%H hours %i minutes %s seconds");
Output
11 hours 32 minutes 4 seconds
$timestamp1 = strtotime('2012-01-26 14:00:00');
$timestamp2 = strtotime('2012-01-25 17:00:00');
if (abs($timestamp1 - $timestamp2) < 60 * 60 * 5 /* (5 hours) */) {
...
Convert both the timestamp and the real time to the UNIX timestamp. Then simply subtract to get the number of seconds difference.
With datetime objects:
$interval = $TempReceiptDateTime->diff($ShipDateTime);
echo $interval->format('%R%H:%I:%s days');
more

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);

Can we use add time to the PHP date() function?

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

Categories