finding start and end date for 2 conditions - php

Basically, my question is about finding date-ranges
From today's date [ using date() ], I want to find
starting and ending date for current week
Let's assume
Today: 14 July, 2011
start of week: 10 July, 2011
end of week: 16 July, 2011
starting and ending date for current month
Let's assume
Today: 14 July, 2011
start of month: 1 July, 2011
end of month: 31 July, 2011
How can I find these dates ?

$firstOfTheMonth = date('1 F Y');
$lastOfTheMonth = date('t F Y');
$startOfTheWeek = date('j F Y', mktime(0, 0, 0, date('n'), date('j') - date('w')));
$endOfTheWeek = date('j F Y', mktime(0, 0, 0, date('n'), date('j') - date('w') + 6));
All the different parameters for date basically give you all you need, the rest is a tiny bit of math.

Related

How To Get Next Wednesday With PHP?

I have this situation :
17 January 2017 is Tuesday.
I'm expecting my code will generate 25 January 2017 as NEXT Wednesday. Not 18 January 2017.
19 January 2017 is Thursday.
I'm expecting my code will generate 25 January 2017 as NEXT Wednesday too.
but this code :
$payment_date = '17 January 2017';
echo $payment_date . '<br>';
$payment_date = date('d M Y', strtotime('next Wednesday', strtotime($payment_date)));
echo $payment_date;
gives me 18 January 2017 as next Wednesday. how to get 25 January 2017 as next Wednesday when my code runs between 15 - 21 January 2017?
thank you
$payment_date = date('d M Y', strtotime('next wednesday next week', strtotime($payment_date)));
Try using +1 week Wednesday instead of Next Wednesday:
$payment_date = date('d M Y', strtotime('+1 week Wednesday', strtotime($payment_date)));

PHP Date function skipping February. Does anybody know of a work around for this date bug?

I am displaying month titles 3 month into the future as well as getting the 1st and last day of each of those months.
for($i = 1; $i < 4; $i++) { // For each month for 3 months
$monthTitle = date('F Y', strtotime('+'.$i.' month'));
$begin_date = date('Y-m-01', strtotime('+'.$i.' month')); // First day of calendar month in future.
$end_date = date('Y-m-t', strtotime('+'.$i.' month')); // Last day of calendar months in future.
};
Nov. 29, 2015 output is:
December 2015
2015-12-01
2015-12-31
January 2016
2016-01-01
2016-01-31
February 2016
2016-02-01
2016-02-29
This was working great right up until yesterday, Nov. 29, 2015 but today Nov. 30, 2015 it skips February.
Nov. 30, 2015 output is:
December 2015
2015-12-01
2015-12-31
January 2016
2016-01-01
2016-01-31
March 2016
2016-03-01
2016-03-31
I'm guessing a bug but does anybody know of a work around?
Thanks to #devlin carnate for pointing me in the right direction.
for($i = 1; $i < 4; $i++) { # for each month
$tmp = date('Y-m-15'); // Get the middle of the month to avoid PHP date bug.
$begin_date = date('Y-m-01', strtotime($tmp . '+'.$i.' month')); // First day of calendar month in future.
$end_date = date('Y-m-t', strtotime($begin_date)); // Last day of calendar months in future.
$monthTitle = date('F Y', strtotime($begin_date));
};
This seems to work very well.
You can use DateInterval to add one month to the current date, so you can get the first and the last day of month.
<?php
$date = new DateTime('2015-12-01');
$i = 0;
while($i < 3){
printf("%s | first day: %s, | last day: %s <br>", $date->format('F Y'), $date->format('d'), $date->format('t'));
$date->add(new DateInterval('P1M'));
$i++;
}
Output:
December 2015 - first day: 01, | last day: 31
January 2016 - first day: 01, | last day: 31
February 2016 - first day: 01, | last day: 29
if last day of next month is needed then you can use this
$d = new DateTime( '2010-01-31' );
$d->modify( 'last day of next month' );
echo $d->format( 'Y-m-d' ), "\n";

Subtracting months from current date returns unexpected result

I just tried to subtract 6 and 5 months respectively from current date 08/29/2015 # 11:19am (UTC) and got the same result
Here is the code sample:
date("M, Y", strtotime("-5 months")) // returns Mar, 2015
date("M, Y", strtotime("-6 months")) // returns Mar, 2015
Is it due to day light saving? I think No.
date("M, Y", strtotime("-6 months"))
Simply returns also Mar, 2015 because there was no 29. February this year. So it takes the next month which is March.
To solve this just do it always from the first day of the month, e.g.
echo date("M, Y", strtotime("-6 months", strtotime(date('Y-m-01'))));
//^^^^^^^^^^^^^^^^^^^^^^^^^ First day of month
This is the only working option i could find for you. How to subtract 4 months from today's date?
echo date("Ymd", mktime(0, 0, 0, date("m")-5, date("d"), date("Y")));;

Why is date() returning the wrong month?

date('F Y', strtotime('2013-05-00T00:00:00')); returns the value 'April 2013', but I would expect it to return 'May 2013'.
Is this an issue with date() interpreting the date string as still in April, or perhaps the format string? I tried 'M Y', which still gave me 'Apr 2013'. Is there an alternative?
date works on a timestamp not a textual representation of a date
If you change the 0 to a 1 for day of month (thus getting a valid date to start from) and use strtotime
date('F Y', strtotime('2013-05-01T00:00:00'));
Then it will work
PHP interprets day of the month 0 as the last day of the previous month.
Valid values for day of the month are 01-31
echo date('d F Y', mktime(0, 0, 0, 5, 0, 2013)); //30 April 2013
echo date('d F Y', mktime(0, 0, 0, 5, 1, 2013)); // 01 May 2013
date('F Y', strtotime('2013-05-00T00:00:00'));
0 is not a valid start date. You have to assign it to 1. Then it will work as you expected.
date('F Y', strtotime('2013-05-01T00:00:00'));

How to find month no,name from week number using php

How to find month no,name from week number using php
If you have the ISO week number, then to get the month (of the start of the week) you can use strtotime like:
// F = full name of month, n = month number without leading zero
echo date('F n', strtotime('2010-W50'));
Bear in mind that the ISO week might not be the same as your meaning of week, so read on.
If you want to count the whole weeks since January 1st of this year (regardless of what day of the week that is) then you could do as Adnan mentioned:
echo date('F n', strtotime('1 Jan + 50 weeks'));
echo date('F',strtotime('1 January 2010 +50 weeks'));
www.php.net/date
www.php.net/strtotime
Have a look at php date() - http://php.net/manual/en/function.date.php
Here are some good examples:
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
$myDate = "2010-05-12";
$weekNumber = date("W", strtotime($myDate));
Just replace the "W" with the value you need. Full reference:
http://php.net/manual/en/function.date.php
If you have a week number, and want the date from it you can use:
date("d m Y", strtotime("1.1.2010 + 30 weeks"));

Categories