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;
?>
get week number and day from date
$input = new \DateTime('2017-07-17');
$firstDayOfMonth = new \DateTime($input->format('Y-m-01'));
$order = (int)(($input->format('j') - 1) / 7) + 1;
function ordinal($number) {
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number%100) <= 13))
return $number. 'th';
else
return $number. $ends[$number % 10];
}
echo ordinal($order).' '.$input->format('l');
You can tinker with the code at https://3v4l.org/dg5Xa
Get week number in month from date in PHP?
I think this relationship should be true and come in handy:
Week of the month = Week of the year - Week of the year of first day of month + 1
We also need to make sure that "overlapping" weeks from the previous year are handeled correctly - if January 1st is in week 52 or 53, it should be counted as week 0. In a similar fashion, if a day in December is in the first week of the next year, it should be counted as 53. (Previous versions of this answer failed to do this properly.)
<?php
function weekOfMonth($date) {
//Get the first day of the month.
$firstOfMonth = strtotime(date("Y-m-01", $date));
//Apply above formula.
return weekOfYear($date) - weekOfYear($firstOfMonth) + 1;
}
function weekOfYear($date) {
$weekOfYear = intval(date("W", $date));
if (date('n', $date) == "1" && $weekOfYear > 51) {
// It's the last week of the previos year.
return 0;
}
else if (date('n', $date) == "12" && $weekOfYear == 1) {
// It's the first week of the next year.
return 53;
}
else {
// It's a "normal" week.
return $weekOfYear;
}
}
// A few test cases.
echo weekOfMonth(strtotime("2020-04-12")) . " "; // 2
echo weekOfMonth(strtotime("2020-12-31")) . " "; // 5
echo weekOfMonth(strtotime("2020-01-02")) . " "; // 1
echo weekOfMonth(strtotime("2021-01-28")) . " "; // 5
echo weekOfMonth(strtotime("2018-12-31")) . " "; // 6
To get weeks that starts with sunday, simply replace date("W", ...)
with strftime("%U", ...)
.
Getting dates for week number last year, php
You could use setISODate for that:
$date = new DateTime();
$date->setISODate(date("Y")-1, date("W"));
$monday = $date->format('Y-m-d');
$friday = $date->modify('+4 days')->format('Y-m-d');
print($monday);
print($friday);
PHP - Get week number from date
Try This.
function weekOfMonth($date) {
//Get the first day of the month.
$firstOfMonth = strtotime(date("Y-m-01", $date));
//Apply above formula.
return intval(date("W", $date)) - intval(date("W", $firstOfMonth)) + 1;
}
You can see it here here
You must execute as
$datee = "2016/08/10";
$datee = strtotime( str_replace("/", "-", $datee));
echo weekOfMonth($datee);
PHP get week numbers and dates by year
I would suggest the following, just change the year to any year you like.
<?php
$year = 2014;
$weeks = getIsoWeeksInYear($year);
for($x=1; $x<=$weeks; $x++){
$dates = getStartAndEndDate($x, $year);
echo $x . " - " . $dates['week_start'] . ' - ' . $dates['week_end'] . "<br>";
}
function getIsoWeeksInYear($year) {
$date = new DateTime;
$date->setISODate($year, 53);
return ($date->format("W") === "53" ? 53 : 52);
}
function getStartAndEndDate($week, $year) {
$dto = new DateTime();
$ret['week_start'] = $dto->setISODate($year, $week)->format('Y-m-d');
$ret['week_end'] = $dto->modify('+6 days')->format('Y-m-d');
return $ret;
}
?>
Get previous week number from php week number
I think I could be over complicating the problem - basically I need to obtain results from the database that appear in the previous weeks from the selected week therefore if there is a possibility of 54 weeks in a year all I really need to do is:-
if($week==1)
{
$year_minsued=$year-1;
$week_minused=54;
}
else
{
$week_minused=$week-1;
$year_minused=$year;
}
My select statement can then read:
SELECT SUM(post_price), SUM(total_price) FROM
database_table
WHERE client_id='$account_no'
AND
(year<='$year_minused' AND week<='$week_minused')
I have not tested this as yet but thinks it should do the job.
Get week number where the weeks start counting from first Monday in April until the first Monday next April in PHP
This seems less convoluted to me, with less processing inside the loop. DateInterval('P7D')
just means set the interval to 7 days (1 week) -- this may be the only part that is mildly confusing because of the syntax. DatePeriod()
does all the hard work for you.
Code (Demo)
$year=2018; // $_GET['year'];
$next=$year+1;
$start=new DateTime(date('Y-m-d', strtotime("first Monday of April $year")));
$stop=new DateTime(date('Y-m-d', strtotime("first Monday of April $next")));
// there is a known behavior of DatePeriod to stop before $stop (...not contain it)
// See http://au2.php.net/manual/en/class.dateperiod.php for explanations & workarounds
$range=new DatePeriod($start,new DateInterval('P7D'),$stop);
echo "<table>";
echo "<tr>";
echo "<th colspan=\"8\">Financial Calendar $year-$next</th>";
echo "</tr>";
echo "<tr>";
echo "<th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th><th>S</th><th>#</th>";
echo "</tr>";
foreach ($range as $i=>$date) {
echo "<tr>";
echo "<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>",++$i,"(",$date->format("Y-m-d"),")</td>";
echo "</tr>";
}
echo "</table>";
Output (unrendered):
<table>
<tr>
<th colspan="8">Financial Calendar 2018-2019</th>
</tr>
<tr>
<th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th><th>S</th><th>#</th>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>1(2018-04-02)</td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>2(2018-04-09)</td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>3(2018-04-16)</td>
</tr>
...
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>50(2019-03-11)</td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>51(2019-03-18)</td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>52(2019-03-25)</td>
</tr>
</table>
Now, I don't want to rob you of the opportunity to develop this code for yourself, so I'll stop here. This should give you a foothold to finish this project to your desired specification.
Related Topics
Php, Display Image with Header()
Uploading in Codeigniter - the Filetype You Are Attempting to Upload Is Not Allowed
Remove .PHP Extension (Explicitly Written) for Friendly Url
Setting Environment Variables for Accessing in PHP When Using Apache
Get Custom Product Attributes in Woocommerce
How to Capture PHP Output into a Variable
Utf-8 Safe Equivalent of Ord or Charcodeat() in PHP
Laravel Eager Loading with Limit
PHP Send E-Mail with Attachment
Composer Installing: the JSON Extension Is Missing
Can You Put PHP Inside PHP with Echo
Executing MySQLi_Query Inside a Function
Laravel Stylesheets and JavaScript Don't Load for Non-Base Routes
How to Install PHP-Curl in Ubuntu 16.04