How to get a day of month from "what I know"? - php

Need some logic here:
Need to get day of month date("d")
What I know:
$year = 2013;
$month = 10;
$week_nr_of_month = 3; // from 1 to 6 weeks in month
$day_of_week = 0; // Sunday date("w")
Thanks for logic
Result must be: 13 October

This was fun to figure out.
<?php
$year = 2013;
$month = 10;
$week_nr_of_month = 3; // from 1 to 6 weeks in month
$day_of_week = 0; // Sunday date("w")
$start = new DateTime();
$start->setDate($year, $month, '1');
$end = clone $start;
$end->modify('last day of this month');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
if ($date->format('w') == $day_of_week) {
$wom = week_of_month($date->format('Y-m-d'));
if ($week_nr_of_month == $wom) {
echo $date->format('Y-m-d');
}
}
}
function week_of_month($date) {
$dt = new DateTime($date);
$bg = clone $dt;
$bg->modify('first day of this month');
$day_of_first = $bg->format('N');
$day_of_month = $dt->format('j');
return floor(($day_of_first + $day_of_month - 1) / 7) + 1;
}
See it in action
Used this answer for inspiration to get the week number for the date.

strtotime may be of help. Can't test right now, but...
$ordinal = array("first","second","third","fourth","fifth");
$weekdays = array("monday","tuesday","wednesday","thursday","friday","saturday","sunday");
$timestamp = strtotime($year."-".$month." ".$ordinal[$week_nr_of_month]." ".$weekdays[$day_of_week]);

Related

How to get Months between two dates

I'm trying to get All the months between two dates.
for e.g if the user posted an article on 21-10-2012 and today date is 5-12-2017. Now i want to get all the month and Year between this period like shown below
10-2012
11-2012
01-2013
02-2014
03-2015
04-2015
05-2015
06-2015
07-2015 // and so on
.......
.......
.......
12-2017 // Till Today Date
Till Now i was only able to calculate the difference.
$article_date= date("d-m-Y", $article['date']);
$year = date("Y");
$month = date("m");
$day = date("d");
$date1 = new DateTime($article_date);
$date2 = new DateTime("$day-$month-$year");
$diff = $date1->diff($date2);
echo (($diff->format('%y') * 12) + $diff->format('%m')) . " full months difference";
How can i get all the months?
Use this code
$start = new DateTime('2012-10-21');
$start->modify('first day of this month');
$end = new DateTime('2017-12-05');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("m-Y") . "<br>\n";
}
function list_months($date_from,$date_to, $return_format){
$arr_months = array();
$a = new \DateTime($date_from);
$x = new \DateTime($date_to);
$start = $a->modify('first day of this month');
$end = $x->modify('first day of next month');
$interval = \DateInterval::createFromDateString('1 month');
$period = new \DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
$arr_months[] = $dt->format($return_format);
}
return $arr_months ;
}
Example: $new_list = list_months('11-10-2012','11-10-2017', 'm-Y');

PHP - How to get days, weeks and months without weekends?

I'm stuck in getting weeks and months using PHP's DateTime::diff function.
Here's my code:
$start = new DateTime('2017-06-05');
$end = new DateTime('2017-06-07');
$end->modify('+1 day');
$interval = $end->diff($start);
// total days
$days = $interval->days;
// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
// best stored as array, so you can add more than one
$holidays = array('2017-09-07');
foreach($period as $dt) {
$curr = $dt->format('D');
// substract if Saturday or Sunday
if ($curr == 'Sat' || $curr == 'Sun') {
$days--;
}
// (optional) for the updated question
elseif (in_array($dt->format('Y-m-d'), $holidays)) {
$days--;
}
}
if($days <= 7) {
echo $days." day".($days > 1 ? 's' : '');
}
// I'm stucked here
I can get the number of working days, but I can't get the months and weeks.
Somebody help me about this?
Try this code
$interval = $date1->diff($date2);
echo $interval->m // month
$week = floor($date1->diff($date2)->days/7);
echo $week; //week

First day between two dates in php

I want to get the first day of the year between two dates, for example if date1 is 2015-02-01 and date2 is 2017-01-07, the answer will be [2015-01-01, 2016-01-01, 2017-01-01]
I have tried the following for the data above:
$date1_rep_val=2015-02-01;
$date2_rep_val=2017-01-07;
$date1=(new DateTime("$date1_rep_val"))->modify('first day of this year');
$date2=(new DateTime("$date2_rep_val"))->modify('first day of this year');
$interval = DateInterval::createFromDateString('1 year');
$period = new DatePeriod($date1, $interval, $date2);
$f_cnt=0;
foreach ($period as $dt)
{
$tick_data[$f_cnt]= $dt->format("Y-m-d");
echo $tick_data[$f_cnt];
$f_cnt++;
}
But, the above code gives only 2015-01-01 and 2016-01-01 it is not giving 2017-01-01 , thanks for any help related to this question.
Why not simply:
$date1 = '2015-02-01';
$date2 = '2017-01-07';
$y1 = substr($date1, 0, 4);
$y2 = substr($date2, 0, 4);
$res= array();
for ($y = $y1; $y <= $y2; $y++) {
$res[] = $y . "-01-01";
}
Alternate (but essentially the same) solution with DateTime type using:
$date1_rep_val='2015-02-01';
$date2_rep_val='2017-01-07';
$date1 = new DateTime($date1_rep_val);
$date2 = new DateTime($date2_rep_val);
$year1 = $date1->format('Y');
$year2 = $date2->format('Y');
$newYearsDates = [];
if (new DateTime($year1 . '-01-01') == $date1) {
$newYearsDates[] = $date1;
}
if ($year2 > $year1) {
for ($year = $year1 + 1; $year <= $year2; $year++) {
$newYearsDates[] = new DateTime($year . '-01-01');
}
}
print_r($newYearsDates);

How can I calculate date weekend in php

I have a date like this
$start = strtotime('2010-01-01'); $end = strtotime('2010-01-25');
My question:
How can I calculate or count weekend from $start & $end date range..??
A more modern approach is using php's DateTime class. Below, you get an array with week numbers as keys. I added the counts of weeks and weekend days.
<?php
$begin = new DateTime('2010-01-01');
$end = new DateTime('2010-01-25');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
$weekends = [];
foreach($daterange as $date) {
if (in_array($date->format('N'), [6,7])) {
$weekends[$date->format('W')][] = $date->format('Y-m-d');
}
}
print_r($weekends);
echo 'Number of weeks: ' . count($weekends);
echo 'Number of weekend days: ' . (count($weekends, COUNT_RECURSIVE) - count($weekends));
Note: if you're using PHP 5.3, use array() instead of block arrays [].
May be this code snippet will help:
<?php
//get current month for example
$beginday = date("Y-m-01");
$lastday = date("Y-m-t");
$nr_work_days = getWorkingDays($beginday, $lastday);
echo $nr_work_days;
function getWorkingDays($startDate, $endDate)
{
$begin = strtotime($startDate);
$end = strtotime($endDate);
if ($begin > $end) {
echo "startdate is in the future! <br />";
return 0;
} else {
$no_days = 0;
$weekends = 0;
while ($begin <= $end) {
$no_days++; // no of days in the given interval
$what_day = date("N", $begin);
if ($what_day > 5) { // 6 and 7 are weekend days
$weekends++;
};
$begin += 86400; // +1 day
};
$working_days = $no_days - $weekends;
return $working_days;
}
}
Another solution can be: (Get date range between two dates excluding weekends)
This might help maybe:
$start = strtotime('2010-01-01');
$end = strtotime('2010-01-25');
$differ = $end-$start;
$min = $differ/60;
$hrs = $min/60;
$days = $hrs/24;
$weeks = $days/7;
if(is_int($weeks))
$weeks++;
echo '<pre>';
print_r(ceil($weeks));
echo '</pre>';

Get month name using start date and end date?

$startDate = "2014-03-01";
$endDate= "2014-05-25";
Result required: March, April, May;
for that PHP delivers the DatePeriod object. Just have a look at the following example.
$period = new DatePeriod(
new DateTime('2014-03-01'),
DateInterval::createFromDateString('1 month'),
new DateTime('2014-05-25')
);
foreach ($period as $month) {
echo strftime('%B', $month->format('U'));
}
A quick solution is to parse each day and check it month:
$startDate = "2014-03-01";
$endDate = "2014-05-25";
$start = strtotime($startDate);
$end = strtotime($endDate);
$result = array();
while ( $start <= $end )
{
$month = date("M", $start);
if( !in_array($month, $result) )
$result[] = $month;
$start += 86400;
}
print_r($result);
I believe it can be done much efficient by new OOP (DateTime object) approach, but this is fast and no-brain if you need to make it work.
<?php
$startDate = "2014-03-01";
echo date('F',strtotime($startDate));
?
$date = date('F',strtotime($startDate));
For full month representation (ie Januaray, February, etc)
$date = date('M',strtotime($startDate));
For abbreviated...(ie Jan, Feb, Mar)
REFERENCE
If you wanna echo out those months in between based on two dates....
$d = "2014-03-01";
$startDate = new DateTime($d);
$endDate = new DateTime("2014-05-01");
function diffInMonths(DateTime $date1, DateTime $date2)
{
$diff = $date1->diff($date2);
$months = $diff->y * 12 + $diff->m + $diff->d / 30;
return (int) round($months);
}
$t = diffInMonths($startDate, $endDate);
for($i=0;$i<$t+1;$i++){
echo date('F',strtotime($d. '+'.$i.' Months'));
}
PHP SANDBOX EXAMPLE

Categories