php within x seconds of y time - php

How can I make a script that can check wether it is currently x seconds from 12am or 12pm?
thanks

You have to get the current timestamp, using the time() function.
Then, you have to get the timestamp of 12am, using for example the strtotime() function.
Then, substract those two values ; and if the absolute value of the result is X, then it's the right time for you ;-)

<?php
$noon = strtotime( "noon" );
$midnight = strtotime( "midnight" );
$timeToMidnight = $midnight - date();
$timeToNoon = $noon - time();
?>

<?php
function timeDiff($firstTime,$lastTime)
{
// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);
// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;
// return the difference
return $timeDiff;
}
//Usage :
echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");
?>

<?php
$time = time();
if( (date("g", $time) % 12) * 3600
+ preg_replace('/^0/', '', date("i", $time)) * 60
+ preg_replace('/^0/', '', date("s", $time))
> $selected_time) {
// do whatever
}
?>

This should work... right?
$time = time();
if ((date("g", $time) == 12) && (date("i", $time) < 10)) {echo 'W00T!';}

Related

how to check if time is more than 30minute?

I have saved a time. I would like to check whether the saved time is 30 minutes greater than the current time. You can see in the code what I have done so far but it doesn't work.
I would need some help fixing that code.
$current = "08:05";
$to_check = date('h:i');
if($to_check > $current + strtotime('30 minute')){
echo "greater";
}
else {
echo "not greater";
}
First, your $current isn't the current time, $to_check is, so your variable names are misleading.
That being said, store your "08:05" as a Unix timestamp then see if the difference between the two is greater than 30 * 60.
$seconds = 1800; // 30 mins
$time1 = strtotime($db_time) + $seconds;
if(time() >= $time1)
echo "Time's up!"
else
echo "time has not expired";
This should work for you:
<?php
$current = date('H:i');
$to_check = date('H:i', strtotime('+30 minutes'));
$to_check = date('H:i', strtotime($record['date'])); //If value is from DB
if($to_check > $current){
echo "greater";
}
else {
echo "not greater";
}
Save time using time function this would give you time in seconds from 1970 so for example current time is 1501137539 and after 30 minutes it would be (1501137539 + 30*60) so you would just need to check if difference b/w current time and stored time is greater that 30*60 then half an hour is over.
Try this snippet:
function x_seconds($to_check = 'YYYY-mm-dd H:i:s') {
$to_check = strtotime($to_check);
$current = time() + 1800; //We add 1800 seconds because it equals to 30 minutes
return ($current>=$to_check) ? true : false;
}
Or go pro, and have a custom "seconds since", just because you can?
function x_seconds($to_check = 'YYYY-mm-dd H:i:s', $seconds = 1800) {
$to_check = strtotime($to_check);
$current = time() + $seconds; //We add x seconds
return ($current>=$to_check) ? true : false;
}
Example usage:
$date = date('Y-m-d H:i:s', '2017-07-27 05:00');
if(x_seconds($date)):
print "Is within 30 minutes.";
else:
print "IS NOT within 30 minutes";
endif;
Try this to solve the issue
$current = strtotime('12:00:00');
$to_check = strtotime(date('H:i:s'));
$newtime = round(( $to_check - $current ) /60 );
if($newtime > 30){
echo "greater";
}else {
echo "not greater";
}

php strtotime in seconds and minutes

i use ths method to find the difference between two timestamp and get the number of seconds between those two times, and i refresh the information with jquery like a counter.
$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = intval(date('s', $diff));
echo $time;
When the difference is more than 60 seconds, the $time comes back to 0, like a reset.
i would like to display 1 min XX s for example
The s flag for date() will never return a value greater than 59 as it only represents the current number of seconds of a given time which can never be more than 59 before rolling over into a new minute.
If you want the total number of seconds you can actually remove your second line of code as the difference between two Unix Timestamps is always in seconds:
$time = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
echo $time;
If you want to display this as minutes and seconds you can use DateTime() which offers better tools for this:
$now = new DateTime();
$then = new DateTime('2014-06-25 14:50:03');
$diff = $now->diff($then);
echo $diff->format('%i minutes %s seconds');
format the date
$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = date('i:s', $diff);
echo $time;
Pass time like 1 & now 2
function diffrencePassTimeAction($DataTime){
$im = $DataTime - strtotime("now");
return $im;
}
Future time like 2 & now 1
function diffrenceFuturTimeAction($DataTime){
$im = strtotime("now") - $DataTime;
return $im;
}
this function delete (-less)
function diffrencePassTimeAction($DataTime){
if ($DataTime > 0)
return $DataTime - strtotime("now");
else
return strtotime("now"); // OR return 0;
}

PHP: Time/HH:Minute:Seconds convert to Seconds

I have a code where it will subtract the Total Duration and the Total Time, and after that the result for the computation will be converted into seconds...
Assuming in my Total Duration is "02:00:00"
then for Total Time is "01:30:00"
For computation...
02:00:00 - 01:30:00 = 00:30:00
then for the result, "00:30:00" will be converted to seconds and the result is "1800"
How can I convert it?
Thanks for the help...
Use strtotime function. It returns the UNIX timestamp (number of seconds since January 1st 1970 00:00:00). If you'll pass the hour format HH:MM:SS to it, you can easily do the math
$to = strtotime('02:00:00');
$from = strtotime('01:30:00');
$seconds = $to - $from; // outputs 30
You assumed that the format is minutes:seconds:miliseconds and you wanted to receive 30 seconds in your case. Actually the output is 30 minutes. Miliseconds are separated with a dot.
Your hours should probably look like this:
$to = strtotime('00:02:00');
$from = strtotime('00:01:30');
How about splitting the Time-String into three substrings with the function (returns an array of substrings)
$substrings = new Array();
$substrings = explode(":", $timeString);
Now the array $substrings contains three substrings (hours, minutes, seconds).
you could compute the seconds just by multiplicating:
$hours = intval($substrings[0]);
$minutes = intval($substrings[1]);
$seconds = intval($substrings[2]);
$seconds = $hours * 3600 + $minutes * 60 + $seconds;
Can you try this,
$start = '01:30:00';
$end = '02:00:00';
$workingHours = (strtotime($end) - strtotime($start));
$res= date("i", $workingHours);
echo "DIFF: ". $res; //OP 30 Minutes
echo $resFull= date("H:i:s", $workingHours); //OP 00:30:00
If you use format HH:MM:SS then you can convert it to seconds by next code
$timestr = "00:30:00";
$temp = explode(":", $timestr);
if ($temp && is_array($temp) && count($temp) == 3) {
$time = intval($temp[0]) * 3600 + intval($temp[1]) * 60 + intval($temp[1]);
} else {
$time = null;
}
Alternative with PHP 5.3:
<?php
try {
$date1 = new DateTime('02:00:00');
$date2 = new DateTime('01:30:00');
$diff = $date1->diff($date2);
echo $diff->format('H:i:s');
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}

Convert dates to hours

I'm trying to work with dates for the first time, I did it something about that with Flash but it's different.
I have two different dates and I'd like to see the difference in hours and days with them, I've found too many examples but not what I'm loking for:
<?php
$now_date = strtotime (date ('Y-m-d H:i:s')); // the current date
$key_date = strtotime (date ("2009-11-21 14:08:42"));
print date ($now_date - $key_date);
// it returns an integer like 5813, 5814, 5815, etc... (I presume they are seconds)
?>
How can I convert it to hours or to days?
The DateTime diff function returns a DateInterval object. This object consists of variabeles related to the difference. You can query the days, hours, minutes, seconds just like in the example above.
Example:
<?php
$dateObject = new DateTime(); // No arguments means 'now'
$otherDateObject = new DateTime('2008-08-14 03:14:15');
$diffObject = $dateObject->diff($otherDateObject));
echo "Days of difference: ". $diffObject->days;
?>
See the manual about DateTime.
Sadly, it's a PHP 5.3> only feature.
Well, you can always use date_diff, but that is only for PHP 5.3.0+
The alternative would be math.
How can I convert it [seconds] to hours or to days?
There are 60 seconds per minute, which means there are 3600 seconds per hour.
$hours = $seconds/3600;
And, of course, if you need days ...
$days = $hours/24;
If you dont have PHP5.3 you could use this method from userland (taken from WebDeveloper.com)
function date_time_diff($start, $end, $date_only = true) // $start and $end as timestamps
{
if ($start < $end) {
list($end, $start) = array($start, $end);
}
$result = array('years' => 0, 'months' => 0, 'days' => 0);
if (!$date_only) {
$result = array_merge($result, array('hours' => 0, 'minutes' => 0, 'seconds' => 0));
}
foreach ($result as $period => $value) {
while (($start = strtotime('-1 ' . $period, $start)) >= $end) {
$result[$period]++;
}
$start = strtotime('+1 ' . $period, $start);
}
return $result;
}
$date_1 = strtotime('2005-07-31');
$date_2 = time();
$diff = date_time_diff($date_1, $date_2);
foreach ($diff as $key => $val) {
echo $val . ' ' . $key . ' ';
}
// Displays:
// 3 years 4 months 11 days
TheGrandWazoo mentioned a method for php 5.3>. For lower versions you can devide the number of seconds between the two dates with the number of seconds in a day to find the number of days.
For days, you do:
$days = floor(($now_date - $key_date) / (60 * 60 * 24))
If you want to know how many hours are still left, you can use the modulo operator (%)
$hours = floor((($now_date - $key_date) % * (60 * 60 * 24)) / 60 * 60)
<?php
$now_date = strtotime (date ('Y-m-d H:i:s')); // the current date
$key_date = strtotime (date ("2009-11-21 14:08:42"));
$diff = $now_date - $key_date;
$days = floor($diff/(60*60*24));
$hours = floor(($diff-($days*60*60*24))/(60*60));
print $days." ".$hours." difference";
?>
I prefer to use epoch/unix time deltas. Time represented in seconds and as such you can very quickly divide by 3600 for hours and divide by 24*3600=86400 for days.

PHP date comparison

How would I check if a date in the format "2008-02-16 12:59:57" is less than 24 hours ago?
if (strtotime("2008-02-16 12:59:57") >= time() - 24 * 60 * 60)
{ /*LESS*/ }
Just adding another answer, using strtotime's relative dates:
$date = '2008-02-16 12:59:57';
if (strtotime("$date +1 day") <= time()) {
// Do something
}
I think this makes the code much more readable.
if ((time() - strtotime("2008-02-16 12:59:57")) < 24*60*60) {
// less than 24 hours ago
}
e.g. via strtotime and time().
The difference must be less then 86400 (seconds per day).
<?php
echo 'now: ', date('Y-m-d H:i:s'), "\n";
foreach( array('2008-02-16 12:59:57', '2009-12-02 13:00:00', '2009-12-02 20:00:00') as $input ) {
$diff = time()-strtotime($input);
echo $input, ' ', $diff, " ", $diff < 86400 ? '+':'-', "\n";
}
prints
now: 2009-12-03 18:02:29
2008-02-16 12:59:57 56696552 -
2009-12-02 13:00:00 104549 -
2009-12-02 20:00:00 79349 +
only the last test date/time lays less than 24 hours in the past.
Php has a comparison function between two date/time objects, but I don't really like it very much. It can be imprecise.
What I do is use strtotime() to make a unix timestamp out of the date object, then compare it with the output of time().
Just use it.....
if(strtotime($date_start) >= strtotime($currentDate))
{
// Your code
}
Maybe it will be more easy to understand...
$my_date = '2008-02-16 12:59:57';
$one_day_after = date('Y-m-d H:i:s', strtotime('2008-02-16 12:59:57 +1 days'));
if($my_date < $one_day_after) {
echo $my_date . " is less than 24 hours ago!";
} else {
echo $my_date . " is more than 24 hours ago!";
}
There should be you variable date Like
$date_value = "2013-09-12";
$Current_date = date("Y-m-d"); OR $current_date_time_stamp = time();
You can Compare both date after convert date into time-stamp so :
if(strtotime($current_date) >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
OR
if($current_date_time_stamp >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
You can use Simple PHP to do this:
$date = new simpleDate();
echo $date->now()->subtractHour(24)->compare('2008-02-16 12:59:57')->isBefore();

Categories