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
Related
I want to implement Birthday countdown time. Normally, birthday normally takes place once in a year eg within 365 days. I downloaded this code from stackoverflow to check the next birthday day of someone who was born on august 22 1982. the days count down should be less than 365 days but the code below is giving a higher value with negative sign.
$rem = strtotime('1982-22-08') - time();
$day = floor($rem / 86400);
$hr = floor(($rem % 86400) / 3600);
$min = floor(($rem % 3600) / 60);
$sec = ($rem % 60);
if($day) echo "$day Days ";
if($hr) echo "$hr Hours ";
if($min) echo "$min Minutes ";
if($sec) echo "$sec Seconds ";
echo "Remaining...";
the next birthday countdown should be less than 365 days
Thanks
Its because you have wrong date format in strtotime and return nothing.
Documentation says
strtotime — Parse about any English textual datetime description into
a Unix timestamp
So if you use
strtotime('08/22/18')
it will work.
Also you should take actual year if you want say "hey your birthday in this is will be..."
What about trying this method:
$birth_month = 8;
$birth_day = 22;
$year = date('Y');
$month = date('n');
$day = date('j');
if ($birth_month < $month || ($birth_month == $month && $birth_day < $day))
$year++;
$target = mktime(0,0,0,$birth_month,$birth_day,$year);
$today = time();
$rem = abs($target - $today);
$days = (int)($rem/86400);
echo "Remaining days till next birthday: $days days!";
I want to get number of weekends and number of bussiness day by I know only $startDate and $endDate, Is have any PHP's function that can calculate automatic ?
This is my code :
$endDate = strtotime($endDate);
$startDate = strtotime($startDate);
echo $days = ($endDate - $startDate) / 86400 + 1;
My code will return how many day, and how I need more is what day ?
How I can get it?
Example:
startDate:`2014-11-17`
endDate:`2014-11-19`
I want this Output:
it's 3 day
2014-11-17 is Monday
2014-11-18 is Tueday
2014-11-19 is Wendnesday
How about...
for ($time = $startDate; $time <= $endDate; $time += 86400) {
echo date('Y-m-d \i\s l', $time) . '<br>';
}
This might help you, or at least may give you some clue
$endDate = strtotime($endDate);
$startDate = strtotime($startDate);
echo $days = ($endDate - $startDate) / 86400 + 1;
$time = $startDate;
while ($time <= $endDate) {
echo date('Y-m-d', $time) . ' is ' . date('l', $time);
$time += 86400;
}
if ($time != $endDate) {
echo date('Y-m-d', $time) . ' is ' . date('l', $time);
}
This should work for you:
<?php
$startDate = strtotime("2014-11-17");
$endDate = strtotime("2014-11-19");
echo "It's " . $days = ($endDate - $startDate) / 86400 + 1 . " days";
for($count = 0; $count < $days; $count++)
echo "<br />" . date('Y-m-d', strtotime('+' . $count . ' day', $startDate)) . ' is ' . date('l', strtotime('+' . $count . ' day', $startDate));
?>
Output:
It's 3 days
2014-11-17 is Monday
2014-11-18 is Tuesday
2014-11-19 is Wednesday
There's no built-in. Excel has NETWORKDAYS(), but in PHP you must roll your own.
There is a PHP solution at Day difference without weekends
Note its limitations in the comments there e.g. if a public holiday falls on a weekend day.
The following page describes an alternative implementation of NETWORKDAYS(): http://www.cpearson.com/excel/betternetworkdays.aspx
It's not PHP but it demonstrates the logic.
Unfortunately there is no shortcut other than looping through each day in the period and deciding whether or not to count it. You need to accept arguments for (or hard-code) the weekend days and the dates of any public holidays (if you are excluding public holidays).
If you are doing this frequently and for long periods, you might pre-compute and cache the number of business days for each calendar month and year; then, at run-time, you look up the number of days in each whole year within the period, then the remaining whole months, then do the ordinary loop for the remaining days at the start and end of the period.
EDIT: If you just want to exclude weekends (not public holidays), then you can calculate 5 days for each whole week in the period, and then calculate any remaining days: https://github.com/bgarlock/scripts/blob/master/PHP%20Equivalent%20of%20MS%20Excel%20NETWORKDAYS%20function.php
Please find below code I am sure it works for you
NOTE : Parameter passed P1D is denoting 1 Day difference same way you can pass P1M for 1 month P1W for 1 week and P1Y for 1 Year.
$date1 = '29/08/2013';
$date2 = '03/09/2013';
function returnDates($fromdate, $todate) {
$fromdate = \DateTime::createFromFormat('d/m/Y', $fromdate);
$todate = \DateTime::createFromFormat('d/m/Y', $todate);
return new \DatePeriod(
$fromdate,
new \DateInterval('P1D'),
$todate->modify('+1 day')
);
}
$datePeriod = returnDates($date1, $date2);
foreach($datePeriod as $date) {
echo $date->format('d/m/Y'), PHP_EOL; //you can set any date format here
}
I have a dynamic date, now what i want is that finding the date after exact one week, i have achieved that with the code below, but now i want that now many days are left for that week after date to come. i have got some sort of time stamp, but i don't know how to convert it to DAYS LEFT.
$weekDate = date( "d/m/Y", strtotime("19-05-2014") + 86400 * 7 );
echo $weekDate;// THATS PERFECT
////////////////////////////////////////////////////////////////
$future = strtotime( $weekDate ); //Future date.
$datediff = time() - $future;
$days = floor( ( ( $datediff / 24 ) / 60 ) / 60 ); //this is not perfect, returns some
sort of timestamp
I have tried other methods which are fine, but if week completes on 26, and today is 25th it gives me 0 days left, but it should say 1 day left. please help me.
In your $date_diff now is less than the future date thats why its zero. Inside strtotime() function, you can directly put a relative date inside. In this case, for one week you can use +1 week or +7 days. Consider this example:
$next_week = date('d/m/Y', strtotime('19-05-2014 +1 week')); // 26/05/2014
$next_week = strtotime('19-05-2014 +7 days');
$difference = $next_week - time(); // next weeks date minus todays date
$difference = date('j', $difference);
echo $difference . (($difference > 1) ? ' days ' : ' day ') . ' left';
// should output: 1 day left
Alright. I did something. Here's the code
$startDate = strtotime("19-05-2014");
$endDate = $startDate + 604800;
$diff = ($endDate - time()) / 60 / 60 / 24;
if ($diff < 1 && $diff > 0) {
$days = 1;
} else {
$days = floor($diff);
}
echo $days;
The problem you have with getting "1 day" if the date is tomorrow is the floor method. strtotime() gives you the time at 0 a.m. if you don't set it by your own. Because of that the difference between now and tomorrow is less than 1 which is 0 if you floor that. I created an if-clause for that.
But that will give you "1 day" for today and "1 day" for yesterday (last 2 days before the final date). If you want that better, you have to specify time in your initial date (19-05-2014).
Use DateTime for date and time calculations.
$weekDate = new \DateTime('+ 1 week');
$future = new \DateTime('+ 3 days');
$daysLeft = $weekDate->diff($future)->days;
echo $daysLeft; //4
See it working.
Reference http://php.net/datetime
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);
}