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.
How to get the total number of week in year php
There is no function to know what date is the maximum week number.
But we know it's in the end of December.
Here I loop a few years and look at the week number on a few dates at the end of the year and pick the maximum value.
for($i = 2017;$i<2030;$i++){
echo $i . ": " . max(date("W", strtotime($i ."-12-27")), date("W", strtotime($i ."-12-29")), date("W", strtotime($i ."-12-31"))). "\n";
}
Output:
2017: 52
2018: 52
2019: 52
2020: 53
2021: 52
2022: 52
2023: 52
2024: 52
2025: 52
2026: 53
2027: 52
2028: 52
2029: 52
https://3v4l.org/18kM1
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);
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;
?>
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;
}
?>
how can I get last week' date range in php?
You can use strtotime()
$previous_week = strtotime("-1 week +1 day");
$start_week = strtotime("last sunday midnight",$previous_week);
$end_week = strtotime("next saturday",$start_week);
$start_week = date("Y-m-d",$start_week);
$end_week = date("Y-m-d",$end_week);
echo $start_week.' '.$end_week ;
UPDATE
Changed the code to handle sunday. If the current day is sunday then - 1 week will be previous sunday and again getting previous sunday for that will go the one week back.
$previous_week = strtotime("-1 week +1 day");
In addition if we need to find the
current week
andnext week
date
range we can do as
Current week -
$d = strtotime("today");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week);
$end = date("Y-m-d",$end_week);
Next Week -
$d = strtotime("+1 week -1 day");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week);
$end = date("Y-m-d",$end_week);
PHP get start and end date of a week by weeknumber
Many years ago, I found this function:
function getStartAndEndDate($week, $year) {
$dto = new DateTime();
$dto->setISODate($year, $week);
$ret['week_start'] = $dto->format('Y-m-d');
$dto->modify('+6 days');
$ret['week_end'] = $dto->format('Y-m-d');
return $ret;
}
$week_array = getStartAndEndDate(52,2013);
print_r($week_array);
Related Topics
Fatal Error: Call to a Member Function Query() on Null
Woocommerce - How to Create Multiple Single Product Template Based on Category
How to Show PHP-Files as Plain Text in Apache
Magento - Redirect Customer from Observer Method
Is Preventing Xss and SQL Injection as Easy as Does This
Multiple Forms and One Processing Page
How to Use Http_X_Forwarded_For Properly
Join Subquery with Doctrine 2 Dbal
Where to Put the PHP Artisan Migrate Command
Magento - Load Only Configurable Products
Why I am Getting Error When Getting Booking from Database
Warning: "Continue" Targeting Switch Is Equivalent to "Break". Did You Mean to Use "Continue 2"