Get week number (per year) from PHP date

I want to take a date and find out what week number.

So far, I still return 24 when it should be 42.

<?php $ddate = "2012-10-18"; $duedt = explode("-",$ddate); $date = mktime(0, 0, 0, $duedt[2], $duedt[1],$duedt[0]); $week = (int)date('W', $date); echo "Weeknummer: ".$week; ?> 

Is it also a coincidence that numbers are reversed? Or am I almost there?

+48
date php mktime
Mar 05 2018-12-12T00:
source share
11 answers

Today, using PHP DateTime is better:

 <?php $ddate = "2012-10-18"; $date = new DateTime($ddate); $week = $date->format("W"); echo "Weeknummer: $week"; 



This is because in mktime() it looks like this:

 mktime(hour, minute, second, month, day, year); 

Therefore, your order is wrong.

 <?php $ddate = "2012-10-18"; $duedt = explode("-", $ddate); $date = mktime(0, 0, 0, $duedt[1], $duedt[2], $duedt[0]); $week = (int)date('W', $date); echo "Weeknummer: " . $week; ?> 
+107
Mar 05 2018-12-12T00:
source share
— -
 $date_string = "2012-10-18"; echo "Weeknummer: " . date("W", strtotime($date_string)); 
+44
Mar 05 2018-12-12T00:
source share

use php date function ... http://php.net/manual/en/function.date.php

date ("W", $ yourdate)

+9
Mar 05 '12 at 13:50
source share

As a suggestion:

 <?php echo date("W", strtotime("2012-10-18")); ?> 

It may be a little easier than this whole lot.

Other things you could do:

 <?php echo date("Weeknumber: W", strtotime("2012-10-18 01:00:00")); ?> <?php echo date("Weeknumber: W", strtotime($MY_DATE)); ?> 
+6
Mar 10 '14 at 10:25
source share

Get the date today, then enter the week number for the week.

 <?php $date=date("W"); echo $date." Week Number"; ?> 
+4
Dec 24 '13 at 18:01
source share

The rule states that the first week of the year is the week that contains the first Thursday of the year.

I personally use Zend_Date for this calculation, and getting a week for today is easy. They have many other useful features if you work with dates.

 $now = Zend_Date::now(); $week = $now->get(Zend_Date::WEEK); // 10 
0
Mar 05 2018-12-12T00:
source share

Your code will work, but you need to flip the 4th and 5th arguments.

I would do it this way

 $date_string = "2012-10-18"; $date_int = strtotime($date_string); $date_date = date($date_int); $week_number = date('W', $date_date); echo "Weeknumber: {$week_number}."; 

In addition, your variable names will confuse you after a week, without looking at this code, you should consider reading http://net.tutsplus.com/tutorials/php/why-youre-a-bad-php-programmer /

0
Mar 05 2018-12-12T00:
source share
 <?php $ddate = "2012-10-18"; $duedt = explode("-",$ddate); $date = mktime(0, 0, 0, $duedt[1], $duedt[2],$duedt[0]); $week = (int)date('W', $date); echo "Weeknummer: ".$week; ?> 

You had the wrong parameters for mktime - should be month / day / year, not day / month / year

0
Mar 05 2018-12-12T00:
source share

Calculation of the week of the week for a given N week ago from the current week in PHP

Get a week (N) ago from the current week of the year

0
Feb 19 '14 at 11:19
source share

try this solution

 date( 'W', strtotime( "2017-01-01 + 1 day" ) ); 
0
Feb 27 '17 at 11:07 on
source share
 function last_monday($date) { if (!is_numeric($date)) $date = strtotime($date); if (date('w', $date) == 1) return $date; else return date('Ym-d',strtotime('last monday',$date)); } $date = '2021-01-04'; //Enter custom date $year = date('Y',strtotime($date)); $date1 = new DateTime($date); $ldate = last_monday($year."-01-01"); $date2 = new DateTime($ldate); $diff = $date2->diff($date1)->format("%a"); $diff = $diff/7; $week = intval($diff) + 1; echo $week; //Returns 2. 
-one
Dec 08 '15 at 8:18
source share



All Articles