Calculate maximum week number in PHP [duplicate] - php

This question already has answers here:
PHP - get last week number in year
(7 answers)
Closed 7 years ago.
I am searching for a way to calculate the maximum week number for a given year in PHP. Since the last week number of a year is defined by whichever week has the first Thursday in the new year, I am not really sure how to do it.
date('W', strtotime( $year.'-12-31 23:59:59'));
could also already return 1, if the 31st of December is a Monday, Tuesday or Wednesday. So I was thinking, maybe do it like this?
date('N', strtotime( $year.'-12-31 23:59:59')) <= 3 ? 52 : 53;
i.e., checking if the last day of the year is a Monday, Tuesday or Wednesday and if so, it's 52 week year, otherwise a 53 week year. Not sure if that's the correct way.

To get the ISO week number (1-53), use :-
idate('W', $timestamp)
or use this :-
$date1 = "2015-12-12";
$date = new DateTime($date1);
$week = $date->format("W");
echo "total week: $week";
or try this :-
date("W", strtotime('2015-12-12'))

Related

GET ISO WEEK NO from month and week no [duplicate]

This question already has answers here:
Calculating days of week given a week number
(14 answers)
Closed 6 years ago.
I am getting month and its weekno like 1,2,3,4,5 . I want ISO week from it.
Eg. I have April as month and week no 4. I want weekno 16 as output.
Thanks in advance.
You can instantiate a new DateTime using the month, increment by the number of weeks, and use the W date format.
<?php
$month = 'April';
$week = 4;
$date = new DateTime("$month 01");
$date->modify("+$week weeks");
echo $date->format('W');
As far as I can tell, the 4th week of April would be ISO week 17, not 16 though.
date("W"); Will return the week number.
http://php.net/manual/en/function.date.php
Use mktime to set a specific date / time and you'll be sorted!

how to format an integer into the name of a weekday in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Is there a way to use DateTime::CreateFromFormat to convert a 0-6 integer into the name of a day of the week?
I am creating a calendar for a script and would like to let users customize the way the days of the week display (Mon vs Monday) so using an array of the week day names is not desirable.
How can I use CreateFromFormat to do this?
Step 1:
Get timestamp of first day of week +X (our integer) days.
// assuming monday = 0, sunday = 6
$day = 2; // our integer (wednesday)
// get timestamp of most recent monday +X days
$ts = (date('N') == 1) ? strtotime("today +$day days") : strtotime("last monday +$day days");
Step 2:
Print the short & long names of the weekday.
echo date('l', $ts); // Wednesday
echo date('D', $ts); // Wed
Hope I understand your question.
$day = 0;
$date = DateTime::createFromFormat('j-M-Y', $day . '-Feb-2010');
echo $date->format('l');
would output :
Sunday

Start Day and End Day Of Current Month & Start Day & End Day of Current Year [duplicate]

This question already has answers here:
The first day of the current month in php using date_modify as DateTime object
(13 answers)
Closed 9 years ago.
I'm trying to get the current start date of the month and current end date of the month.
It needs to change for each month, so for example for this month I need:
$startMonth = 2014-02-01
$endMonth = 2014-02-28
I'm aware of the mktime PHP page but I can't get my head around it: http://uk3.php.net/mktime
I also need the start day and end day of the current year:
$startYear = 2014-01-01
$endYear = 2014-12-31
I need it in the above format, I can get the previous months using:
$previousmonthStart = date("Y-m-d", mktime(0, 0, 0, date("m"), 0, date("Y")));
$previousmonthEnd = date("Y-m-d", mktime(0, 0, 0, date("m")-1, 1, date("Y")));
Just need to know what changes what.
Since you want to use mktime()...
I think the first day of the year and the last day of the current year are always (no mktime() needed):
date('Y') . '01-01';
date('Y') . '12-31';
For the first and last of the current month with mktime() try this:
$m = (integer) date('n');
$start = date('Y-m-d',mktime(1,1,1,$m,1,date('Y')));
$end = date('Y-m-d',mktime(1,1,1,++$m,0,date('Y')));
//Considering today is 2014-02-05
echo $start; //2014-02-01
echo $end; //2014-02-28
mktime wants hour, min, sec, month, day ,year
$m tells mktime to use the current month for the month value and the next 1 tells it to use the first day of that month.
++$m tells it to use the next month and the 0 gets you the day before the first day of the next month which is the last day of the current month.
Example: http://codepad.org/1G7UJNni

Getting first day of the month -1 month [duplicate]

This question already has answers here:
how to get the first and last days of a given month
(10 answers)
Closed 9 years ago.
How do I get the first day of the month in php? And the first day of one month early.
So as example:
$startdate = 01-05-2014
$enddate = 01-06-2014
The enddate has to be the first day of the current month so if it is 26-01-2014 the enddate is 01-01-2014
and the startdate is then 01-12-13
As soon as you have the dates as a Timestamp, you can use this to get the first day of the month:
date("Y-m-1", $timestamp) ;
You can get the last day of a month, when you use:
date("t", $timestamp) ; // would give you 31 in January
In a complete example for your problem:
$timestamp = strtotime("26-01-2014") ;
$startdate = date("Y-m-1", strtotime("-1 month", $timestamp)) ; // to subtract one month -> use strtotime
$enddate = date("Y-m-1", $timestamp) ;
See also in the documentation of PHP for strtotime and date.

Finding first and last day of the week (or month, quarter, or year) [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Grab current first and last day in week in php
I am trying to get the first and last day of the week, month, quarter, and year for a given day.
For instance, for day 2013-01-16, the first and last days would be:
Week - It would be 2013-01-13 and 2013-01-19 (assuming Sunday to Saturday)
Month - It would be 2013-01-01 and 2013-01-31
Quarter - It would be 2013-01-01 and 2013-03-31
Year - It would be 2013-01-01 and 2013-12-31
My purpose is to include them in a WHERE myDate BETWEEN 2013-01-13 AND 2013-01-19.
Both a standard MYSQL function solution (but not stored procedure, etc) or PHP solution is acceptable. Thank you
Solution for first and last day of given quarter.
$q=ceil($date->format("n")/3);
$months_start=array('January','April','July','October');
$months_end=array('March','June','September','December');
$m_start=$months_start[$q-1];
$m_end=$months_end[$q-1];
$modifier='first day of '.$m_start.' '.$date->format('Y');
$date->modify($modifier);
echo $modifier.': '.$date->format('Y-m-d').'<br>';
$modifier='last day of '.$m_end.' '.$date->format('Y');
$date->modify($modifier);
echo $modifier.': '.$date->format('Y-m-d').'<br>';
You can use strtotime:
$date = strtotime('2013-01-16');
// First/last day of week
$first = strtotime('last Sunday');
$last = strtotime('next Saturday');
Or PHP's native DateTime functionality:
$date = new DateTime('2013-01-16');
// First/last day of month
$first = $date->modify('first day of this month');
$last = $date->modify('last day of this month');
Getting the first/last day of a year might be a little bit more tricky:
// Get date
$date = new DateTime('2013-01-16');
// Format to get date
$year = $date->format('Y');
// Get first day of Jan and last day of Dec
$first = $date->modify("first day of January $year");
$last = $date->modify("last day of December $year");

Categories