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 />";
}
Related
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/>';
}
Basically am trying to set a time and a date in PHP then set a time gap which will range between minutes, loop through between a start time and end time echoing something out for each one. Have tried loads of different ways and cant seem to figure a way to set a date and add to it.
This seems the best script I have modified so far:
$minutes = 5;
$endtime = new DateTime('2012-01-01 09:00');
$newendtime = $endtime->format('Y-m-d H:i');
$timedate = new DateTime('2012-01-01 09:00');
while($stamp < $newendtime)
{
$time = new DateTime($timedate);
$time->add(new DateInterval('PT' . $minutes . 'M'));
$timedate = $time->format('Y-m-d H:i');
echo $timedate;
}
$minutes = 5;
$endtime = new DateTime('2012-01-01 09:00');
//modified the start value to get something _before_ the endtime:
$time = new DateTime('2012-01-01 8:00');
$interval = new DateInterval('PT' . $minutes . 'M');
while($time < $endtime){
$time->add($interval);
echo $time->format('Y-m-d H:i');
}
Do everything in seconds, and use php's time(), date(), and mktime functions.
In UNIX Time, dates are stored as the number of seconds since Jan 1, 1970.
You can render UNIX Timestamps with date().
$time = time(); // gets current time
$endtime = mktime(0,0,0, 1, 31, 2012); // set jan 31 # midnight as end time
$interval = 60 * 5; // 300 seconds = 5 minutes
while($time < $endtime){
$time += $interval;
echo date("M jS Y h:i:s a",$time) . "<br>"; // echos time as Jan 17th, 2012 1:04:56 pm
}
date reference:
http://us3.php.net/manual/en/function.date.php (includes superb date format reference too)
mktime reference: http://us2.php.net/mktime
time() only gets the current time, but just for kicks n' giggles: http://us2.php.net/time
And, it's super easy to store in a database!
This function will let you add date to your existing datetime. This will also preserves HH:MM:SS
<?php
function add_date($givendate,$day=0,$mth=0,$yr=0) {
$cd = strtotime($givendate);
$newdate = date('Y-m-d h:i:s', mktime(date('h',$cd),
date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
date('d',$cd)+$day, date('Y',$cd)+$yr));
return $newdate;
}
?>
Usage:
add_date($date,12,0,0);
where $date is your date.
I have two epochs. I want to figure out all the dates that are valid within the two epochs.
For example, if I have the epochs 946684800 (Sat, 01 Jan 2000 00:00:00 GMT) and 947203200 (Fri, 07 Jan 2000 00:00:00 GMT), I want to be able to get: 01/01/2000, 02/01/2000, 03/01/2000, 04/01/2000, etc.
If you have PHP 5.3 or newer, you could do this:
$date1 = new DateTime;
$date1->setTimestamp(946684800);
$date2 = new DateTime;
$date2->setTimestamp(947203200);
$interval = new DateInterval('P1D');
while ( $date1 <= $date2 )
{
$dates_in_between[] = $date1->getTimestamp();
$date1->add($interval);
}
Alternatively, you could use this:
// 1 day = 60 seconds * 60 minutes * 24 hours = 86400
for ($date = 946684800; $date <= 947203200; $date += 86400)
$dates_in_beteween[] = $date;
$dates_in_between will contain a list of "dates" in between.
PHP time values are just Unix timestamps - seconds since Jan 1/1970. Going off PHP 5's datetime object:
$start = strtotime('01 Jan 2000');
$end = strtotime('07 Jan 2000');
for ($d = $start; $d <= $end; $d += 86400) { // increment by 86,400 seconds, aka 1 day
echo date('d/m/Y', $d);
}
There's better ways of going about it, using the DateTime / DateInterval objects, but this is just to show the basics.
Given that your epoch is in seconds you could always add the number of seconds found in a day to the first epoch:
946684800 + 86400 = 946771200 -> Sun, 02 Jan 2000 00:00:00 GMT
And go on like this, I explain better:
947203200 - 946684800 = 518400 / 86400 = 6 (exactly 6 days)
so (PSEUDOCODE):
for(int i = 946684800; i<946684800 ;i+=86400){
day = getDate(i);
}
$epoch1 = '946684800';
$epoch2 = '947203200';
$i = 0;
while($time < $epoch2) {
$time = mktime(0, 0, 0, date("m", $epoch1) , date("d", $epoch1)+$i, date("Y",$epoch1));
echo date('d/m/Y', $time)."<br>";
$i++;
}
If understanding the question right, you want every DAY within the 2 epochs (2000-01-01 and 2000-01-07)..
Can be done like so:
<?php
$epoch1 = 946684800;
$epoch2 = 947203200;
$difference = $epoch1 - $epoch2;
..
//count days
$amountOfDays = round(($epoch2-$epoch1)/86400);
//looping all days
for($i=1; $i<=$amountOfDays; $i++) {
echo date('d/m/Y', $epoch1+($i*86400);
}
?>
$start = strtotime('2011-06-01');
$end = strtotime('2011-06-15');
$date = $start;
$anArray = array();
while ($date <= $end) {
$date = strtotime("+1 DAY", $date);
$anArray[] = $date;
}
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
I am trying to get an array of a date plus the next 13 dates to get a 14 day schedule starting from a given date.
here is my function:
$time = strtotime($s_row['schedule_start_date']); // 20091030
$day = 60*60*24;
for($i = 0; $i<14; $i++)
{
$the_time = $time+($day*$i);
$date = date('Y-m-d',$the_time);
array_push($dates,$date);
}
But it seems to be repeating a date when the month switches over..
this is what I get:
2009-10-30|2009-10-31|2009-11-01|2009-11-01|2009-11-02|2009-11-03|2009-11-04|2009-11-05|2009-11-06|2009-11-07|2009-11-08|2009-11-09|2009-11-10|2009-11-11
Notice that 2009-11-01 is repeated. I cannot figure out why?
What am I doing wrong?
Thanks!!
I would use strtotime
$start = strtotime($s_row['schedule_start_date']);
$dates=array();
for($i = 1; $i<=14; $i++)
{
array_push($dates,date('Y-m-d', strtotime("+$i day", $start)));
}
print_r($dates);
You have the same date because of daylight saving time switch. It's not safe to add 24*60*60 seconds to find next day, because 2 days in the year have more/less seconds in them. When you switch from summer to winter time you are adding 1 hour to a day. So it'll be 25*60*60 seconds in that day, that's why it's not switched in your code.
You can do your calculation by mktime(). For example:
## calculate seconds from epoch start for tomorrow
$tomorrow_epoch = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
## format result in the way you need
$tomorrow_date = date("M-d-Y", $tomorrow_epoch);
Or the full version for your code:
$dates = array();
$now_year = date("Y");
$now_month = date("m");
$now_day = date("d");
for($i = 0; $i < 14; $i++) {
$next_day_epoch = mktime(0, 0, 0, $now_month, $now_day + $i, $now_year);
array_push(
$dates,
date("Y-m-d", $next_day_epoch)
);
}
I recommend something like:
for($i=1;$i<=14;$i++){
echo("$i day(s) away: ".date("m/d/Y",strtotime("+$i days")));
}