I want a First Day Of a week say I have 45th week. Now I want to have date of the first sunday of this week.
Can any one suggest me how I can go about this ?
Thank you
Searching around a bit, the most suggested is:
$year = "2009"; // date("Y");
$week = "45"; // date("W");
$firstDayOfWeek = strtotime($year."W".str_pad($week,2,"0",STR_PAD_LEFT));
print("The first day of week ".$week." of ".$year." is ".date("D, d-m-Y",$firstDayOfWeek));
Basically this comes down to letting strtotime do the work for you. You fill in "2009W45" and it'll retrieve the date for you.
Pitfall here is that the week needs to be in a 2 digit format. So week 1 needs to be 01, hence the str_pad to zero-fill it.
you should strtotime and date function
some code like that
$next_sunday = strtotime('next sunday');
$next_week = strtotime('+1 week');
$next_sunday_of_next_week = strtotime('next sunday', $next_week);
hope this helps
If you have the month, day and year:
$timestamp = mktime(0, 0, 0, $month, $day, $year);
echo date('c', $timestamp) = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year);
you could also do it this way if you have the timestamp:
echo $date('c', mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
From: http://pinoytech.org/blog/post/get-the-first-day-of-the-week-with-an-exact-date
date($str_format, strtotime($year."W".$week."1"))
Where $str_format is the output formate according to the PHP date() function.. I use 'M d Y'
Related
I know that I can use date("w"); to get a number 0-6 depending on what day it is, but how would I do this to see what day the first day in the current month is? For example, this month March 1st was a Wednesday, so it should return a 3 for Wednesday.
I tried using this date("w", "Y-m-01") but it just gives me the error
Warning: date() expects parameter 2 to be long, string given
This returns the day (eg. "Wednesday") that is first in the current month:
echo date('l',mktime(0, 0, 0, date('m'), 1));
..I suspect this was more useful to OP, hence the accept, but in fact it can be modified to meet the exact requirements of the question (number - eg "3") with:
echo date('N',mktime(0, 0, 0, date('m'), 1));
<?php
$month_start = strtotime('first day of this month', time());
//first day name , month, year
echo date('D, m Y', $month_start).'<br/>';
//first day number (Monday = 1, Tuesday = 2) etc , month, year
echo date('N, m Y', $month_start).'<br/>';
I have tried this:
$date = date("N", mktime(0, 0, 0, date('n'), 1, date('Y')));
echo $date;
you can do it with class DateTime
echo (new DateTime('first day of this month'))->format('jS, F Y');
echo (new DateTime('2017-03-17'))
->modify('first day of this month')
->format('jS, F Y');
I am trying to find the next 10th of a month starting with today. So if today is the 27th of May, the next 10th is the 10th of June. If today is the 1st of August, the next 10th will be the 10th of August for example.
I know I can find the first day of the next month using
$firstDayNextMonth = date('Y-m-d', strtotime('first day of next month'));
Can I use that format for my case as well? If not, how could I achieve my goal?
Thanks a lot for helping! :)
strtotime is not able to do this in one call, but you could do something like:
<?php
$current_day = (int)date('j');
if ($current_day < 10) {
$firstDayNextMonth = date('Y-m-d', strtotime('+9 days', strtotime('first day of this month')));
} else {
$firstDayNextMonth = date('Y-m-d', strtotime('+9 days', strtotime('first day of next month')));
}
echo $firstDayNextMonth;
?>
Or simply just
$firstDayNextMonth = date('Y-m-d', strtotime('+9 days', strtotime('first day of ' . ((int)date('j') < 10 ? 'this' : 'next' ) . ' month')));
You can use the DateTime class:
$Date = new DateTime('first day of '.(date('j') < 10 ? 'this' : 'next').' month');
$Date->add(DateInterval::createFromDateString('9 days'));
var_dump($Date->format('Y-m-d'));
This will result in '2015-07-10' if it's '2015-06-10' today. Just use <= (or < 11) if it should return '2015-06-10' in that case.
Not sure if strtotime is able to do that.
A little trick can be:
$firstDayNextMonth = date('Y-m', strtotime('next month')) . '-10';
This might help a bit?
By using mktime you can simply define the day you want, even match more day sin a row more easely.
// set our day number
$daynum = 10;
$curDay = date('d');
// get current month
$curMonth = date('n');
// get current year
$curYear = date('Y');
$firstDayNextMonth = 'MissingNo';
if($curDay < $dayNum) {
$firstDayNextMonth = mktime(0, 0, 0, $curMonth, $daynum);
}
else {
// darnit, new year. but will we party?
if ($curMonth == 12) {
// use mktime to get a timestamp. We can become very precise with this...
$firstDayNextMonth = mktime(0, 0, 0, 0, $daynum, $curYear+1);
}
else {
$firstDayNextMonth = mktime(0, 0, 0, $curMonth+1, $daynum);
// let's test if we can have a little party or not.
}
}
echo date('D',$firstDayNextMonth) === date('D') ? 'matches! let\'s party!' : 'no match :( hide the beer';
$firstDayNextMonth = date('Y-m-d', strtotime('first day of next month'));
echo $firstDayNextMonth ."</br>";
$firstDayNextMonth = date('Y-m', strtotime('next month'));
echo $firstDayNextMonth."-10";
Is that what you want??
Very simple:
date('Y-m-d', strtotime('+9 days', strtotime('first day of this month', strtotime(sprintf('+%d days', (int) date('t') - 10)))));
I have some data that makes use of date("j/n/y") format i.e date for today is 23/1/15
I have tried
echo strtotime($today);
but this does not give me the timestamp i want.How would i convert a date in date("j/n/y") format to epoch?.
Use DateTime::createFromFormat() to read the date format and then use DateTime::getTimestamp() to format it as a timestamp.
$date = DateTime::createFromFormat('j/n/y', '23/1/15');
$epoch = $date->getTimestamp();
I think you're looking for the mktime function in PHP. It goes a little like this:
$timestamp = mktime(0,0,0,0,0,0);
Where, in order, the arguments are: hour, minute, second, month, day, year. So, in your case:
$today = mktime(0, 0, 0, 1, 23, 2015);
// Would return the timestamp for Jan. 23rd, 2015 at 12:00:00 am (I think)
If you're looking for a dynamic right now timestamp, you may use date() in each of the arguments of mktime. For example:
$rightnow = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
// Would return the timestamp for Jan. 23rd, 2015 at 10:57:25 am.
But, as John Conde says, it requires you break apart the date before you can use it, so it may not be as efficient.
Hope that helps!
Just to have another approach this one would be good for 85 more years.
$date = date('j/n/y', time());
list($day, $month, $year) = explode("/", $date);
$date = "20" . $year . "-" . $month . "-" . $day;
echo date('m/d/Y', strtotime($date));
I have a problem with find the current date from past mktime. In PHP I find the current date using date("j");. Here I need, suppose my date was in the past year like mktime(0, 0, 0, 2, 1, 2008), then here how can I find the current date of this particular past month.
Either as #octern's solution, or you can do
$day = date('j', strtotime("-2 months"));
or
$day = date('j', strtotime('-30 days'));
depending on your need.
You may also want to refer to strtotime() manual.
Try this:
$date = getdate(mktime(0, 0, 0, 2, 1, 2008));
$day = $date['mday'];
Or just:
$day = date('j', mktime(0, 0, 0, 2, 1, 2008))
I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?
$first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));
See date() in PHP documentation.
First day is always YYYY-MM-01, isn't it? Example: date("Y-M-d", mktime(0, 0, 0, 8, 1, 2008))
Last day is the previous day of the next month's first day:
$date = new DateTime("2008-09-01");
$date->modify("-1 day");
echo $date->format("Y-m-d");
The first day of the month is always 1.
So it will become
YYYY-MM-01
the last day can be calculated as:
<?php
$num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There was $num days in August 2003";
?>
OK, first is dead easy.
date ('Y-m-d', mktime(0,0,0,MM,01,YYYY));
Last is a little trickier, but not much.
date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY));
If I remember my PHP date stuff correctly...
**edit - Gah! Beaten to it about a million times...
Edit by Pat:
Last day should have been
date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1
<?php
echo "Month Start - " . $monthStart = date("Y-m-1") . "<br/>";
$num = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));
echo "Monthe End - " . $monthEnd = date("Y-m-".$num);
?>
The easiest way to do this with PHP is
$dateBegin = strtotime("first day of last month");
$dateEnd = strtotime("last day of last month");
echo date("MYDATEFORMAT", $dateBegin);
echo "<br>";
echo date("MYDATEFORMAT", $dateEnd);
Or the last week
if (date('N', time()) == 7) {
$dateBegin = strtotime("-2 weeks Monday");
$dateEnd = strtotime("last Sunday");
} else {
$dateBegin = strtotime("Monday last week");
$dateEnd = strtotime("Sunday last week");
}
Or the last year
$dateBegin = strtotime("1/1 last year");
$dateEnd = strtotime("12/31 this year");
By the way #ZombieSheep solution
date ('Y-m-d', mktime(0,0,0,$MM + 1,-1,$YYYY));
does not work it should be
date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1
Of course #Michał Słaby's accepted solution is the simplest.
Just to verify that I didn't miss any loose ends:
$startDay = 1;
if (date("m") == 1) {
$startMonth = 12;
$startYear = date("Y") - 1;
$endMonth = 12;
$endYear = date("Y") - 1;
}
else {
$startMonth = date("m") - 1;
$startYear = date("Y");
$endMonth = date("m") - 1;
$endYear = date("Y");
}
$endDay = date("d") - 1;
$startDate = date('Y-m-d', mktime(0, 0, 0, $startMonth , $startDay, $startYear));
$endDate = date('Y-m-d', mktime(0, 0, 0, $endMonth, $endDay, $endYear));
try this to get the number of days in the month:
$numdays = date('t', mktime(0, 0, 0, $m, 1, $Y));
Example; I want to get first day and last day of current month.
$month = (int) date('F');
$year = (int) date('Y');
date('Y-m-d', mktime(0, 0, 0, $month + 1, 1, $year)); //first
date('Y-m-d', mktime(0, 0, 0, $month + 2, 0, $year)); //last
When you run this for instance at date 2015-01-09, the first and last values will be sequentially;
2015-01-01
2015-01-31
Tested.
From here(get next month last day) that is marked as duplicated, so i can't add comment there, but people can got bad answers from there.
Correct one for last day of next month:
echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-t'));
Correct one for first day of next month:
echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-01'));
Code like this will be providing March from January, so that's not what could be expected.
echo ((new DateTime())->modify('+1 month')->format('Y-m-t'));
proper way to build a relative date from now is:
//bad example - will be broken when generated at 30 of December (broken February)
echo date("Y-m-d", strtotime("now"))."\n";
echo date("Y-m-d", strtotime("now + 1 month"))."\n";
echo date("Y-m-d", strtotime("now + 2 month"))."\n";
echo date("Y-m-d", strtotime("now + 3 month"))."\n";
//good example, you can change first day to last day or any day
echo date("Y-m-d", strtotime("first day of this month"))."\n";
echo date("Y-m-d", strtotime("first day of next month"))."\n";
echo date("Y-m-d", strtotime("first day of +2 month"))."\n";
echo date("Y-m-d", strtotime("first day of +3 month"))."\n";
and the result will be:
2021-12-30
2022-01-30
2022-03-02
2022-03-30
2021-12-01
2022-01-01
2022-02-01
2022-03-01