this code get count hours between tow times
ineed out put like this
6:00:00AM
6:30:00AM
7:00:00AM
7:30:00AM
8:00:00AM
8:30:00AM
......
10:30:00PM
how i can do this
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$diff = date_diff($end, $start);
print_r($diff->h);
Alternatively you could use DatePeriod to build the list of dates on the desired specification, as opposed to iterating over comparisons of the date and adding the increment.
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$interval = \DateInterval::createFromDateString('30 minutes');
$periods = new \DatePeriod($start, $interval, $end);
foreach ($periods as $date) {
echo $date->format('h:i:sA') . \PHP_EOL;
}
Result: https://3v4l.org/FnEae
06:00:00AM
06:30:00AM
07:00:00AM
07:30:00AM
08:00:00AM
08:30:00AM
//..
10:30:00PM
Since \DateTime::diff() won't always show the total hours between dates with different days, you can count the hours between the two dates by using iterator_count on the DatePeriod of the desired interval:
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$interval = \DateInterval::createFromDateString('1 hour');
$periods = new \DatePeriod($start, $interval, $end);
echo iterator_count($periods) . ' hours';
Result: https://3v4l.org/P75K7
17 hours
<?php
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$halfHour = new DateInterval('PT1800S');
while ($start < $end) {
print ($start->format('h:i:sA').PHP_EOL);
$start->add($halfHour);
}
?>
Create 2 Datetime objects and a date interval object and iterate while less than the end Datetime add the interval each time.
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$interval = new DateInterval('PT30M');
while ($start <= $end) {
echo $start->format('H:i:s');
$start->add($interval);
}
Related
I need to find out count of a specific Day between two date.
I can do this by using loop between two date, but if there is difference of 10 years(suppose) between date loop will run 10*365 times.
Is any easier way to do this?
Thanks in advance
function countDays($day, $start, $end)
{
$start = strtotime($start);
$end = strtotime($end);
$datediff = $end - $start;
$days = floor($datediff / (60 * 60 * 24));
//get the day of the week for start and end dates (0-6)
$w = array( date('w', $start ), date('w', $end) );
//get partial week day count
if ($w[0] < $w[1])
{
$partialWeekCount = ($day >= $w[0] && $day <= $w[1]);
}else if ($w[0] == $w[1])
{
$partialWeekCount = $w[0] == $day;
}else
{
$partialWeekCount = ($day >= $w[0]
$day <= $w[1]);
}
//first count the number of complete weeks, then add 1 if $day falls in a partial week.
return intval($days / 7) + $partialWeekCount;
}
Function Call - countDays( 5, "2017-04-14", "2017-04-21")
You are looking for date_diff() function. The date_diff() function returns the difference between two DateTime objects.
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
To find occurrence of specific day (for eg: Sunday). Please note: I have used 7 for Sunday. You can use 1 for Monday, 2 for Tuesday and so on
$cnt = 0;
$start = new DateTime("2013-03-15");
$end = new DateTime("2013-12-12");
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
if ($dt->format('N') == 7)
{
$cnt++;
}
}
echo $cnt;
I need to create a date-time with specific day each month between a start date and a end date.
Example :
Specific day : 04
Start : 2016-12-22
End : 2017-03-06
Output needed : 2016-01-04 ; 2016-02-04 ; 2016-03-04
How can I do that?
You can create a DatePeriod object and iterate over it to get all the dates you need:
$start = new DateTime('2016-12-22');
$end = new DateTime('2017-03-06');
// Compute the occurrence on the same month as $start
$first = new DateTime();
$first->setDate($start->format('Y'), $start->format('m'), 4);
// If it is in the past of $start then the first occurrence is on the next month
if ($first < $start) {
$first->add(new DateInterval('P1M'));
}
// Create a DatePeriod object that iterates between $first and $end
// in increments of 1 month
$period = new DatePeriod($first, new DateInterval('P1M'), $end);
// Run through the list, process each item
foreach ($period as $day) {
echo($day->format('Y-m-d')."\n");
}
I found it ! This following code is working for me :
$d1 = new DateTime("2016-12-22");
$d2 = new DateTime("2017-03-06");
$dd = '04';
if ($dd < $d1->format('d') ) {
$d1->add(new \DateInterval('P1M'));
$date = $dd.'-'.$d1->format('m').'-'.$d1->format('Y');
} else {
$date = $d1->format('Y').'-'.$d1->format('m').'-'.$dd;
}
$date = new DateTime($date);
print_r($date);echo('<br />');
while ($date->add(new \DateInterval('P1M')) <= $d2){
$d1->add(new \DateInterval('P1M'));
$date = $dd.'-'.$d1->format('m').'-'.$d1->format('Y');
$date = new DateTime($date);
print_r($date);echo('<br />');
}
for example I have two dates 2015-10-28 and 2015-12-31. from these I want to know how many saturday and sunday in that given date range. I can find the diff between that dates but I can't find how many weekends.
anyone ever made this?
here is my current code:
function createDateRange($maxDate, $cell, $lead, $offArray = array()){
$dates = [];
--$cell;
--$lead;
$edate = date('Y-m-d', strtotime($maxDate." -$lead day"));
$sdate = date('Y-m-d', strtotime($edate." -$cell day"));
$start = new DateTime($sdate);
$end = new DateTime($edate);
$end = $end->modify('+1 day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach($period as $d){
$dt = $d->format('Y-m-d');
if(!in_array($dt, $dates)){
$dates[] = $dt;
}
}
return $dates;
}
basically I want to add sat+sun count to the date range.
The trick is to use an O(1)-type algorithm to solve this.
Given your starting date, move to the first Saturday. Call that from
Given your ending date, move back to the previous Friday. Call that to
Unless you have an edge case (where to is less than from), compute (to - from) * 2 / 7 as the number of weekend days, and add that to any weekend days passed over in steps (1) and (2).
This is how I do it in production, although generalised for arbitrary weekend days.
Use this function:
function getDateForSpecificDayBetweenDates($startDate, $endDate, $weekdayNumber)
{
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
$dateArr = array();
do
{
if(date("w", $startDate) != $weekdayNumber)
{
$startDate += (24 * 3600); // add 1 day
}
} while(date("w", $startDate) != $weekdayNumber);
while($startDate <= $endDate)
{
$dateArr[] = date('Y-m-d', $startDate);
$startDate += (7 * 24 * 3600); // add 7 days
}
return($dateArr);
}
The function call to get dates for all Sunday's in year 2015:
$dateArr = getDateForSpecificDayBetweenDates('2015-01-01', '2015-12-31', 0);
print "<pre>";
print_r($dateArr);
//周日0 周一1 .....
$data = 4;//周四
$t1 ='2015-10-28';
$t2 = '2015-12-31';
$datetime1 = date_create($t1);
$datetime2 = date_create($t2);
$interval = date_diff($datetime1, $datetime2);
$day = $interval->format('%a');
$result = ($day)/7;
$start = getdate(strtotime($t1))['wday'];
$end = getdate(strtotime($t2))['wday'];
if($data>=$start && $data<=$end){
echo floor($result)+1;
}else{
echo floor($result);
}
Here's the case.
I store vacation information of employees in mysql in unix timestamp.
What I want to do is to
- show the vacation days in a calendar;
- show how much time passed since last vacation;
The result I'm trying to get is like in this picture:
http://postimg.org/image/7tdw4hmwf/
The problem is my calendar is starting from 31-12-1969 and showing too many days.
Here's the code:
$vac_begin = '1412100000';
$vac_end = '1412877600';
// $vac_end = '1414087200';
$ve = new DateTime(date("d-m-Y", $vac_end));
$today = new DateTime(date('d-m-Y'));
$time_passed = $today->diff($ve)->format('%y years %m months');
echo 'Vacation start:'.date("d-m-Y", $vac_begin).'<br/>';
echo 'Vacation end:'.date("d-m-Y", $vac_end).'<br/>';
echo 'Time passed since last vacation: '.$time_passed.'<br/>';
//---------------------------------------
echo 'Days to show in calendar<br/>';
//Get monday of the week
$before = date("N", $vac_begin) - 1;
//Get sunday of the week
$after = 7 - date("N", $vac_end);
//Vacation days
$datediff = $vac_end - $vac_begin;
//Get number of remaining days to show in calendar
$remaining = 42 - (floor($datediff/(60*60*24)) + 1 + $before + $after);
//Allocate remaining days before and after vacation period
$floor = floor($remaining/7/2);
$round = round($remaining/7/2);
if($before > $after){
$after += $round*7;
$before += $floor*7;
} else{
$after += $floor*7;
$before += $round*7;
}
$format_sdate = date("d-m-Y", $vac_begin);
$format_edate = date("d-m-Y", $vac_end);
//Get first day to show in calendar
$cal_start_date = date("d-m-Y", strtotime($format_sdate.' -'.$before.' days'));
//Get last day to show in calendar
$cal_end_date = date("d-m-Y", strtotime($format_edate.' +'.$after.' days'));
$begin = new DateTime($cal_start_date);
$end = new DateTime($cal_end_date);
$end = $end->modify('+1 day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $dt){
echo $dt->format("d").', ';
}
echo $cal_start_date;
I have a database with a possible 10 dates in input into each entry. The User will choose a start and end date and I want all the dates to be inputted into the database for my calender page.
$date1 = '2013-01-31';
$date2 = '2013-02-05';
for($one = $date1;$one>$date2;$one = strtotime(date('m/d/Y',$one)." -1 day"))
{
echo $one;
}
firstly I need to extract all the dates. The above does not work. Meaning dates 31/01/02/03/04/05 need to be inputted into the database.
DateTime makes this so much easier:
For PHP 5.3+ Users
$start = new DateTime('2013-01-31');
$end = new DateTime('2013-02-05');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
echo $dt->format("m/d/Y") . PHP_EOL;
}
For PHP 5.2 Users
$start = new DateTime('2013-01-31');
$end = new DateTime('2013-02-05');
while ($start <= $end)
{
echo $start->format("m/d/Y") . PHP_EOL;
$start->modify("+1 day");
}
reference
DateTime
DatePeriod
DateInterval