Get week number (in the year) from a date PHP

Today, using PHP's DateTime objects is better:

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

It's because in mktime(), it goes like this:

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

Hence, 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;
?>

Use PHP's date function
http://php.net/manual/en/function.date.php

date("W", $yourdate)

$date_string = "2012-10-18";
echo "Weeknummer: " . date("W", strtotime($date_string));

Tags:

Php

Date

Mktime