In PHP, how do you know how many months have passed this month uptil today?

Suppose today is February 21, 2011 (Monday). This is the third Monday of this month. If the date is indicated as input, how can I find out how many Mondays have passed before?

In PHP, how do you know how much has passed today this month?

+1
source share
6 answers
$now=time() + 86400; if (($dow = date('w', $now)) == 0) $dow = 7; $begin = $now - (86400 * ($dow-1)); echo "Mondays: ".ceil(date('d', $begin) / 7)."<br/>"; 

works for me ....

EDIT: includes today and Monday.

+4
source

This seems like a fairly simple division calculation. Subtract the number of days from past Mondays from the current date (example: wednesday = -2), divide it by 7 and ceil() to round it.

EDIT: This will include the current Monday in the room, returning "3" on Monday 21st.

+3
source

You can scroll all days to the present day and count Mondays:

 $firstDate = mktime(0, 0, 0, date("n"), 1, date("Y")); $now = time(); $mondays = 0; for ($i = $firstDate; $i < $now; $i = $i + 24*3600) { if (date("D", $i) == "Mon") $mondays ++; } 

Not tested this script

0
source
  <?php function mondays_get($month, $stop_if_today = true) { $timestamp_now = time(); for($a = 1; $a < 32; $a++) { $day = strlen($a) == 1 ? "0".$a : $a; $timestamp = strtotime($month . "-$day"); $day_code = date("w", $timestamp); if($timestamp > $timestamp_now) break; if($day_code == 1) @$mondays++; } return $mondays; } echo mondays_get('2011-02'); 

I hope this is useful to you! I just rolled it.

"Beware of errors in the above code, I just proved it right, haven't tried it."

Powered by OK afaik

0
source

Try it...

 //find the most recent monday (doesn't find today if today is Monday though) $startDate = strtotime( 'last monday' ); //if 'last monday' was not this month, 0 mondays. //if 'last monday' was this month, count the weeks $mondays = date( 'm', $startDate ) != date( 'm' ) ? 0 : floor( date( 'd', $startDate ) / 7 ); //increment the count if today is a monday (since strtotime didn't find it) if ( date( 'w' ) == 1 ) $mondays++; 
0
source

Another way is to find which day of the week today, find the first such day of the month through strtotime() magic, and then calculate the difference between this and now in weeks. See below a function that will accept Ymd formatted date() and return that day of the week of the month.

Note: strtotime must be verbose, including the β€œfrom” and the month: β€œfirst Monday 2011-02”, otherwise β€œit’s moving forward one day . This is a bit of me when I tested edge cases.

Also added some display pepper, which is completely optional, but I liked it.

 function nthWeekdayOfMonth($day) { $dayTS = strtotime($day) ; $dayOfWeekToday = date('l', $dayTS) ; $firstOfMonth = date('Y-m', $dayTS) . "-01" ; $firstOfMonthTS = strtotime($firstOfMonth) ; $firstWhat = date('Ym-d', strtotime("first $dayOfWeekToday of $monthYear", $firstOfMonthTS)) ; $firstWhatTS = strtotime($firstWhat) ; $diffTS = $dayTS - $firstWhatTS ; $diffWeeks = $diffTS / (86400 * 7); $nthWeekdayOfMonth = $diffWeeks + 1; return $nthWeekdayOfMonth ; } $day = date('Ym-d') ; $nthWeekdayOfMonth = nthWeekdayOfMonth($day) ; switch ($nthWeekdayOfMonth) { case 1: $inflector = "st" ; break ; case 2: $inflector = "nd" ; break ; case 3: $inflector = "rd" ; break ; default: $inflector = "th" ; } $dayTS = strtotime($day) ; $monthName = date('F', $dayTS) ; $dayOfWeekToday = date('l', $dayTS) ; echo "Today is the {$nthWeekdayOfMonth}$inflector $dayOfWeekToday in $monthName" ; 
0
source

Source: https://habr.com/ru/post/1393552/


All Articles