I iterate through time by adding 86400 (the count of seconds in the 1 day) to list the dates.
define("DAY_SEC", 86400);
for ($i = strtotime("2016-10-01"); $i<=strtotime("2016-11-05"); $i = $i + DAY_SEC){
echo Date("Y-m-d, H:i:s", $i). ' = '. $i . '<br/>';
}
But look on the date "2016-10-30". It is there twice but with different hours
2016-10-28, 00:00:00 = 1477605600
2016-10-29, 00:00:00 = 1477692000
2016-10-30, 00:00:00 = 1477778400 <- here
2016-10-30, 23:00:00 = 1477864800 <- here
2016-10-31, 23:00:00 = 1477951200
What do I wrong?
thanx
Do not add or subtract or do any other math with timestamps to calculate past or future dates. Your code will fail after 2016-11-05 in U.S. and some other time zones because that is the end of Daylight Saving Time this year. strtotime() is aware of this, so use it with "+1 day" or something similar:
for ($i = strtotime("2016-10-01"); $i<=strtotime("2016-11-05"); $i = strtotime("+1 day", $i)){
echo Date("Y-m-d, H:i:s", $i). ' = '. $i . '<br/>';
}
It turns out 2016-10-30 is the end of Daylight Saving Time in many other places, so the time zone you are using is subject to that change.
It is called DST(Daylight Savings Time). Try using strtotime().
$start = strtotime("2016-10-01");
$end = strtotime("2016-11-05");
$interval = 1; //in days
for ($i = $start; $i<=$end; $i = $i + strtotime("+".$interval." days", $i)){
echo Date("Y-m-d, H:i:s", $i). ' = '. $i . '<br/>';
}
Related
This question already has answers here:
Print time in 15-minute increments between two times in the same day
(10 answers)
Closed 11 months ago.
I want to create a dropdown that will take current time as start time and will end up till 24 hours, like till coming 24 hours so in between it will show time of every 15 minutes increment. The problem is that when I try to run the loop starting time is okay but with next loop the time jumps to 6 hours behind.
Here is my code:
$current_time = date('h:i A');
$end_hour = date("+24 hours", $current_time);
echo "<option>" . $current_time . "</option>";
for($i = 0; $i <= 96; $i++) {
echo "<option>" . date("h:i A", $tNow) . "</option>";
$tNow = strtotime('+15 minutes',$current_time);
}
Output coming as
11:08 PM
4:00 PM
4:15 PM
4:30 PM
And so on.
You can use DateTime for that:
$now = new DateTime();
$end = clone $now;
$end->modify("+24 hours");
while ($now <= $end) {
echo "<option>" . $now->format('h:i A'). "</option>";
$now->modify('+15 minutes');
}
there are a couple of things. First on your first line you are missing the second parameter. Then you are using $tNow undefined.
$current_time = date('h:i A', time());
$end_hour = date("+24 hours", strtotime($current_time));
$tNow = strtotime($current_time);
echo "<option>" . $current_time . "</option>";
for($i = 0; $i <= 96; $i++) {
echo "<option>" . date("h:i A", $tNow) . "</option>";
$tNow = strtotime('+15 minutes', $tNow);
}
I am trying to output this in PHP as a UNIX timestamp instead:
05-30-2014 0:00:00
05-30-2014 23:59:59
05-29-2014 0:00:00
05-29-2014 23:59:59
05-28-2014 0:00:00
05-28-2014 23:59:59
05-27-2014 0:00:00
05-27-2014 23:59:59
05-26-2014 0:00:00
05-26-2014 23:59:59
05-25-2014 0:00:00
05-25-2014 23:59:59
05-24-2014 0:00:00
05-24-2014 23:59:59
This should be dynamic, and always show the previous seven days.
So, I am trying to get the start, and end, of each of the last seven days as a timestamp.
I was able to get the dates to output, but when including times and converting to timestamps I think it is getting messed up.
EDIT
I have this so far, which gets me the correct starting date and times, but this needs to be converted to a timestamp.
$timestamp = time()- 3600 * 24;
for ($i = 0 ; $i < 7 ; $i++) {
echo date('Y-m-d 00:00:00', $timestamp) .'<br />';
echo date('Y-m-d 23:59:59', $timestamp) .'<br />';
$timestamp -= 24 * 3600;
}
When I try changing it to a timestamp, and then use an online converter to reverse the timestamp it's always off by 5 hours. For example, I took the timestamp for midnight of a given date, and checked it in the online converter to see if it was correct, but it was saying 5am instead of midnight.
$date = new DateTime();
for($i = 0; $i < 7; $i++) {
$date->modify("last day");
$date->setTime(0,0,0);
echo $date->getTimestamp() . "<br/>\n";
$date->setTime(23,59,59);
echo = $date->getTimestamp() . "<br/>\n";
}
edit
If you want to have a specific timezone anyway, then change my first line above into:
$date = new DateTime("now", new DateTimeZone("Europe/Amsterdam"));
And set your desired timezone in stead of Europe/Amsterdam.
edit 2
Your own code that you have put in your own edit isn't that bad. You just needed to add strtotime() to turn your date string into a timestamp:
$timestamp = time()- 3600 * 24;
for ($i = 0 ; $i < 7 ; $i++) {
echo strtotime(date('Y-m-d 00:00:00', $timestamp)) .'<br />';
echo strtotime(date('Y-m-d 23:59:59', $timestamp)) .'<br />';
$timestamp -= 24 * 3600;
}
And don't be scared of what an online converter says. The online converter could be in a different timezone.
$start_timestamp = mktime(0, 0, 0, date("m"), date("d"), date("y"));
$end_timestamp = $start_timestamp+86399;
//run it 7 times
for ($x=0; $x<7; $x++) {
$start_timestamp = (int) $start_timestamp - 86400;
$end_timestamp = $start_timestamp+86399;
echo $end_timestamp."<br />".$start_timestamp."<br />";
}
I am coming here with a fairly simple (i think) question today. I would like to figure out the past seven days with php
So: I have todays date in variables (like $day = 2, $month = 5, $year = 2013, all put together 2/5/2013), My question is How would i go about getting the past seven days (in the same format) such as in this case
2/5/2013
1/5/2013
30/4/2013
29/4/2013
28/4/2013
27/4/2013
26/4/2013
I have tried subtracting the days for each variable (like $day6 = $todays_date - 1;) but getting month and year changes from that would be quite difficult i believe.
any answers would be appreciated.
Use DateTime class and its modify method
$date = new DateTime();
$yesterday = $date->modify('-1 day');
You could use mktime, and subtract the number of seconds for each day:
$today = mktime(0,0,0,$month,$day,$year);
for($i=0;$i<=6;$i++){
echo date('j/n/Y',$today-($i*(24*60*60))) . '<br />;
}
Try this:
$day = '02/05/2013';
$dates = array();
for($i = 0; $i < 7; $i++){
$dates[] = date('Y-m-d', strtotime($day . ' -' . $i . 'days'));
}
print_r('<pre>');
print_r($dates);
die();
I developed a function to obtain equal time intervals within a user generated time-frame.
$start = strtotime("12:00"); //start at...
$end = strtotime("18:00"); //end at...
$timeframe = $end - $start; //time-frame
$intervals = 3; //number of intervals within time-frame
$interval_time = $timeframe/$intervals; //time of each interval
for($i = 0, $start;
$i < $intervals;
$i++, $start = strtotime("+$interval_time seconds", $start)) //increment time
{
$new_time = date('H:i:s a', $start);
echo "$new_time \n";
}
The code above outputs
12:00:00 pm 14:00:00 pm 16:00:00 pm
What code can I incorporate to divide each interval and obtain the middle? For example to get from the code above
13:00:00 pm 15:00:00 pm 17:00:00 pm
Please kindly advise :)
Replace your for loop with:
$half_interval = $interval_time / 2;
$mid = $start + $half_interval;
for ( $i = 1; $i < $intervals; $i ++) {
echo date('H:i:s a', $mid) . " \n";
$mid += $interval_time;
}
echo date('H:i:s a', $mid) . " \n";
Admittedly a simple approach. I'm sure improvements are possible, but leave them as an exercise for the reader! ;-)
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