I have software that needs to determine if the cutoff datetime is greater than 24 hours from now. Here is the code I have to test that.
$date = strtotime("2013-07-13") + strtotime("05:30:00");
if($date > time() + 86400) {
echo 'yes';
} else {
echo 'no';
}
My current date and time is 2013-07-13 2am. As you can see its only 3 hours away.
At my math thats 10800 seconds away. The function I have is returning yes. To me this is saying the $date is greater than now plus 86400 seconds when in fact its only 10800 seconds away. Should this not be returning no?
$date = strtotime("2013-07-13") + strtotime("05:30:00");
should be
$date = strtotime("2013-07-13 05:30:00");
See difference in this CodePad
<?php
date_default_timezone_set('Asia/Kolkata');
$date = "2014-10-06";
$time = "17:37:00";
$timestamp = strtotime($date . ' ' . $time); //1373673600
// getting current date
$cDate = strtotime(date('Y-m-d H:i:s'));
// Getting the value of old date + 24 hours
$oldDate = $timestamp + 86400; // 86400 seconds in 24 hrs
if($oldDate > $cDate)
{
echo 'yes';
}
else
{
echo 'no'; //outputs no
}
?>
Store the values of date and time in separate variables and convert it into a Unix timestamp using strtotime() after concatenating the variables.
Code:
<?php
$date = "2013-07-13";
$time = "05:30:00";
$timestamp = strtotime($date." ".$time); //1373673600
if($timestamp > time() + 86400) {
echo 'yes';
} else {
echo 'no'; //outputs no
}
?>
Related
I get the value 9.0 from a CSV file that I would like to print as the time 09:00:00
If i try
<?php
$time = 9.01;
echo date('H:i:s', strtotime($time));
It displays 09:01:00.
But if I try
<?php
$time = 9;
echo date('H:i:s', strtotime($time)); or echo date('H:i:s', strtotime(9.00));
It displays 01:00:00.
I want to get time 09:00:00.
Please try this
$time = 9;
echo date( 'H:i:s', strtotime( number_format($time, 2)));
This is how to fill a date variable with any value you need:
<?php
$d=mktime(9, 0, 0);
echo "Created date is " . date("h:i:s", $d);
?>
See the documentation of mktime
It appears the main issue is the import CSV has a malformed date. Assuming you cannot change it and that the value is {hours}.{minutes}. Minutes not being a fraction but a value between 0 - 60. Otherwise, this will need adjusting to convert the fraction of an hour into minutes.
I'm using the DateTime class to achieve this.
$value = 9.1;
if (is_float($value)) {
$hour = (int)$value;
$minute = number_format(($value - floor($value)) * 100);
$date = new DateTime();
$date->setTime($hour, $minute);
} else {
$date = new DateTime($value);
}
$formattedTime = $date->format('H:i');
echo $formattedTime . "\n";
For 12 hours Formate
echo date("h:i:s a");
For 24 hours Formate
echo date("H:i:s");
I need to add 15 minutes to the current time.
For eg : now is 20:48 , need to add 15 minutes, so now it will be 21:03, but i need to set 21:15 ,that is , it should be in multiples of15,30,45,00.
Help help/guidance would be of good help.
<?php
$current_date_time = date('d/m/Y H:i:s');
$current_date = date("d/m/Y H:i", strtotime($current_date_time."+15 minutes"));
echo $current_date;exit;
Here's a simple example
//what time is it?
$t=time();
//how long is our interval?
$interval=15*60;
//we can calculate when the last interval started by subtracting $t % $interval
$last = $t - $t % $interval;
//so now we know when the next interval will start...
$next = $last + $interval;
echo "Next interval at ".strftime('%H:%M:%S', $next)."\n";
You look like you might want to add 2*$interval to the last interval, but you should be able to adapt this to suit you.
Just to correct my post:
$time = time();
$last_time = ($time - ($time % (15 * 60)));
$in15mins = $last_time+ (15 * 60);
echo 'Now: '. date('Y-m-d H:i') ."\n";
echo 'in 15 mins: '. date('Y-m-d H:i', $in15mins) ."\n";
I think it is quite self explaining. Optimize it, use it.
$current_date_time = date('d/m/Y H:i:s');
echo $current_date_time."<br>";
$current_date = date("d/m/Y H:i", strtotime($current_date_time."+15 minutes"));
echo $current_date."<br>";
$minutes = date("i",strtotime($current_date));
$min = '';
if($minutes > 0 && $minutes <15){
$min = 15 - $minutes;
} else if($minutes > 15 && $minutes <30){
$min = 30 - $minutes;
} else if($minutes > 30 && $minutes <45){
$min = 45 - $minutes;
} else {
$min = 59 - $minutes;
$min++;
}
$newdate = date("d/m/Y H:i", strtotime($current_date."+".$min." minutes"));
echo $newdate;
Use the above code. this is working for me.
$currentTimeStamp=strtotime('now');
$nextInterval=date("Y-m-d H:i", strtotime("+15 minutes", $currentTimeStamp));
dump($nextInterval);
How can I get the last/previous half hour mark using PHP date/time.
So the time 15:31 all the way through to 16:29 should give 15:30 since that's the last half hour mark.
And I want to be able to dynamically change the minutes.
So if I set the minutes to 15, it'll get the last/previous :15 minute mark
So 15:16 through to 16:14 will return 15:15
This is what I have but I just can't get it to work...
function newtime($time, $offset) {
$prev = $time - $offset;
$newtime = ($offset > 900) ? ($prev - 1800) : ($prev);
return $newtime;
}
$hours = date("H", time());
$minutes = date("m", time());
echo 'Current date: ' . date("Y-m-d H:i:s", time()) . '<br/>';
$time = strtotime("2013-01-23 15:35:00");
echo 'time: ' . date("Y-m-d H:i:s", $time) . '<br/>';
$offset = ($time % 1800);
echo 'prev: ' . $offset . '<br/>';
$newtime = newtime($time, $offset);
echo date("Y-m-d H:i:s", $newtime) . '<br/><br/>';
It's really tough, I just can't wrap my head around it.
Any help is greatly appreciated!
Try something like this:
<?
function newtime($time,$minute=30){
$time=strtotime($time);
$m=date("i",$time)*1;
$h=date("H",$time)*1;
if($m<$minute){
$h=$h-1;
}
return date("H:i",strtotime($h.":".$minute));
}
print newtime("15:31");
//OUTPUT: 15:30
print newtime("16:29");
//OUTPUT: 15:30
print newtime("15:16",15);
//OUTPUT: 15:15
print newtime("16:14",15);
//OUTPUT: 15:15
?>
function newtime ($time, $offset)
{
// First, calculate the offset in seconds:
$offset_sec = $offset * 60;
// Next, fetch unix timestamp from $time
$unix_time = strtotime($time);
// Then calculate the modulo
$modulo = $unix_time % $offset_sec;
// Calculate latest timestamp
$last_time = $unix_time - $modulo;
// Display latest timestamp
return date('H:i',$last_time);
}
//Example data
$current_time = 1318075950;
$unbanned_time = $current_time + strtotime('+1 minute');
if ($unbanned_time > $current_time) {
$th1is = date('Y-m-d H:i:s', $unbanned_time) - date('Y-m-d H:i:s', $current_time);
echo date('Y-m-d H:i:s', $th1is);
I am trying to output how long time it is until the user is unbanned... year months, days, hours, minutes and seconds... But this is giving me some weird results..
You should check manual on how to work with date/time functions.
First of all, instead of
$current_time + strtotime('+1 minute')
use
strtotime('+1 minute', $current_time);
(see manual on strtotime).
Secondly, date function returns a string. Subtracting two strings is not really useful in most cases.
if ($unbanned_time > $current_time) {
$th1is = $unbanned_time - $current_time;
echo $th1is/3600 . ' hours';
}
This will output the remaining time in hours but there are many functions available that will produce better formatting (or you can code one for yourself).
I would recommend to use DateTime
$DateTime = new DateTime();
$unbanned_DateTime = new DateTime();
$unbanned_DateTime = $unbanned_DateTime->modify('+1 minute');
if ( $unbanned_DateTime > $DateTime ) {
$interval = $DateTime->diff($unbanned_DateTime);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%d');
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$seconds = $interval->format('%s');
}
Instead of using every single value as variable you can use ->format() for one output. As you like.
Remember DateTime->format() needs a timezone setting up in your php.ini or with
date_default_timezone_set('....');
date() returns a string, substracting two strings makes no sense here. You can use basic maths to calculate the remaining time:
<?php
$current_time = time();
$unbanned_time = /* whatever */;
$seconds_diff = $unbanned_time - $current_time();
echo "You're unbanned at " . date("Y-m-d H:i:s", $unbanned_time) . " which is over ";
if ($seconds_diff <= 120) {
echo "$seconds_diff seconds";
} else if ($seconds_diff <= 7200) {
echo floor($seconds_diff / 60) . " minutes";
} else if ($seconds_diff <= 7200 * 24) {
echo floor($seconds_diff / 3600) . " hours";
} else {
echo floor($seconds_diff / 3600 / 24) . " days";
}
?>
Hay, i have a database holding events. There are 2 fields 'start' and 'end', these contain timestamps. When an admin enters these dates, they only have the ability to set the day,month,year. So we are only dealing with stamps containing days,months,years, not hours,minutes,seconds (hours,minutes and seconds are set to 0,0,0).
I have an event with the start time as 1262304000 and the end time as 1262908800. These convert to Jan 1 2010 and Jan 8 2010. How would i get all the days between these timestamps? I want to be able to return Jan 2 2010 (1262390400), Jan 3 2010 (1262476800) .. all the way to the end stamp. These events could cross over into different months, say May 28 to June 14.
Any ideas how to do this?
You just have to calculate the number of seconds between the two dates, then divide to get days :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
Then, you can use a for loop to retrieve the dates :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
for ($i = 1; $i < $numDays; $i++) {
echo date('Y m d', strtotime("+{$i} day", $smallestTimestamp)) . '<br />';
}
Again, if you don't know which timestamp is the smallest, you can use the min() function (second argument in strtotime).
I think that a quick workaround for this is to subtract the amount of a days worth of seconds from the end_stamp until you get to the start_tag.
//1 day = 86400 seconds
I would build an array of the days to use later.
EDIT (example)
$difference = 86400;
$days = array();
while ( $start_time < $end_time )
{
$days[] = date('M j Y', $end_time);
$end_time -= $difference;
}
This should cover any time frame even if its over a bunch of months.
Try this:
while($date_start <= $date_end) {
echo date('M d Y', $date_start) . '<br>';
$date_start = $date_start + 86400;
}
Hope this helps !
$d1=mktime(22,0,0,1,1,2007);
$d2=mktime(0,0,0,1,2,2007);
echo "Hours difference = ".floor(($d2-$d1)/3600) . "<br>";
echo "Minutes difference = ".floor(($d2-$d1)/60) . "<br>";
echo "Seconds difference = " .($d2-$d1). "<br>";
echo "Month difference = ".floor(($d2-$d1)/2628000) . "<br>";
echo "Days difference = ".floor(($d2-$d1)/86400) . "<br>";
echo "Year difference = ".floor(($d2-$d1)/31536000) . "<br>";
http://www.plus2net.com/php_tutorial/date-diff.php
http://www.phpf1.com/tutorial/php-date-difference.html
$daysInBetween = range($startTs, $endTs, 86400);
$secondDay = date('M d Y', $daysInBetween[1]);
/*
$thirdDay = date('M d Y', $daysInBetween[2]);
...
*/
Note that the range() function is inclusive.
**This is a very simple code for find days hours minutes and seconds in php**
$dbDate = strtotime("".$yourbdDate.""); // Database date
$endDate = time(); // current time
$diff = $endDate - $dbDate; /// diffrence
$days = floor($diff/86400); /// number of days
$hours = floor(($diff-$days*86400)/(60 * 60)); //// number of hours
$min = floor(($diff-($days*86400+$hours*3600))/60);///// numbers of minute
$second = $diff - ($days*86400+$hours*3600+$min*60); //// secondes
if($days > 0) echo $days." Days ago";
elseif($hours > 0) echo $hours." Hours ago";
elseif($min > 0) echo $min." Minute ago";
else echo "Just second ago";
Something like this?
$day = $start;
while ($day < $end) {
$day += 86400;
echo $day.' '.date('Y-m-d', $day).PHP_EOL;
}
By the way, 1262304000 is Dec 31, not Jan 1.
get the difference of two dates and divide it by 86400. abs(($date1 - $date2) / 86400) will produce the needed result