I am trying to calculate, based on today's date (24 August 2012), the following three values:
Date of this month's first Saturday.
Date of this month's third Saturday.
Date of next month's first Saturday.
This is how I do it in PHP script:
// Returns August 2012
$this_month = date("F Y");
// Returns September 2012
$next_month = date("F Y", strtotime("next month"));
// Returns August 04th, Saturday
$t_sat_1st=date('U', strtotime($this_month.' First Saturday'));
// Returns August 18th, Saturday
$t_sat_3rd=date('U', strtotime($this_month.' Third Saturday'));
// Returns September 08th, Saturday
$n_sat_1st=date('U', strtotime($next_month.' First Saturday') );
Why is the wrong date returned for the last line of code? I expect it to return September 1st, 2012. What is wrong with my code?
I don't know why your code doesn't exactly work, must be the way it is parsed..
but try
$n_sat_1st=date('U', strtotime('first saturday of ' . $next_month) )
Note the 'of' it is necessary.
This code will only work in 5.3+
It should be noted that apparently some of these strings only work in PHP 5.3 apparently, notably:
"first day of this month" and "last day of this month" for example.
According to information found on another website, the "xxx day of"
feature was added in PHP 5.3.
Comment on this page - http://www.php.net/manual/en/datetime.formats.relative.php
I have tested this on both windows and ubuntu both php 5.3 and 5.4 and it works.
If this does not work, try this
$d = new DateTime($next_month);
$d->modify('first saturday of this month');
echo $d->format('U');
Related
I need to fetch previous month from the current month passed. Tried below code and it does work for the month having 30 days but does not work for specific months having 31 days viz. March, May, July, October and December
Note: The question may sound repeated, but please read it completely till the end. You can check the same issue by changing the system and testing below code against it. I need the previous month output in format Jul
For Date: 30-Jul output is
Previous Month-Jun
Current Month-Jul
For Date: 31-Jul output is
Previous Month-Jul
Current Month-Jul
$prev_month = date('M', strtotime("last month"));
echo 'Previous Month--'.$prev_month;
echo 'Current Month--'.date('M');
Also tried echo date('M', strtotime("-1 Months")); but it outputs the same as above.
If current month is (August) with 31 days and so previous month is (July) with 31 days then it works and shows correct Previous Month i.e. July, but it does not work if current month has 31 days and previous month has 30 or lesser days.
How should I go about it to fetch correct previous month on the basis of current month ?
You simply try as
echo "Previous Month".date('M',strtotime('first day of last month'));
and for specific date you can simply use
echo date('M',strtotime('first day of last month',strtotime('30-Jun')));//May
You should pass in the date from the start of the month.
$startDate = date('Y-m-1');
$prevMonth = date('M', strtotime("last month", strtotime($startDate)));
see
PHP date() and strtotime() return wrong months on 31st
I need the names of the day (Monday, Tuesday, Wednesday, Thuesday, Friday, Saturday, Today).
I know this is a newby question and PHP has a date() function. But I tried and can't figure out how...
According to the PHP Manual at http://php.net/manual/en/function.date.php, just use "l" as the format parameter to get the full name of the day.
So 23rd Mar 2014 is a Sunday, as echoed by
<?php
echo date ("l", mktime(0, 0, 0, 3, 23, 2014));
// Echoes Sunday
?>
To get past 7, 6, 5 or 10000 days (or number of days in the future) from the current day, according the information at this page, just use negative or positive integers in a string in the strtotime function:
<?php
$backcount = -4;
echo date ("l", strtotime("$backcount day"));
// Executed on 23 Mar 2014 will give Wednesday
?>
Knowing this, you can apply a for loop to get what you need. And if want "Today" instead of the full name of the current day, just add an if condition to handle the situation where the backcount variable is zero.
Achieving this using the DateTime Class and its format method.
The below code's output changes every day.. Since today is Sunday it starts from Monday , Tuesday... If you run this code on Tuesday , you will be getting output as Thursday , Friday , Saturday .. so on.
<?php
for($i=1;$i<=7;$i++) //<--- Since we know total days in a week is 7.
{
$date = new DateTime(); //<-- Grabs today's datetime
$date->add(new DateInterval('P'.$i.'D')); //<--- Passes the current value of $i to add days..
echo $date->format('l')."<br>";
}
OUTPUT :
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Working Demo
you can use jddayofweek to retrieve day of week, it has 3 mode for string containing the day of week, string containing the abbreviated day of week and int represent number of day in week
for($i=0;$i<7;$i++){
$x=jddayofweek($i,2);
var_dump($x);
}
I'm looking for a reliable way to return the full date of a specified weekday (e.g. "Mon") for the current week.
Since today is Wednesday, June 13, 2012, I expected <?php echo date("Y-m-d", strtotime('Mon this week')); ?> to result in 2012-06-11, but instead php returns 2012-06-18 as though it interprets this week as meaning next week. Why this behavior and what should I be doing?
Thanks.
--Jeff
date( 'Y-m-d', strtotime( 'last Monday', strtotime( 'Sunday' ) ) );
This searches for the Monday previous to the next Sunday.
According to the documentation php relative date formats.
Then Monday this week would first advance to the next Monday and then process the relative text of this week.
dayname: Moves to the next day of this name unless it is the current day then it will not advance. In other words if the current date was June 11, then strtotime('Monday this week') would return June 11 whereas if the current date was June 13 then strtotime('Monday this week') would return June 19.
i think this is the solution for your problem:
$monday_date = date("Y-m-d", mktime(0,0,0, date("m"), date("j")-(date("w")+1), date("Y")));
im looking for the DateTime modify String for the first day of the year (now 1. January 2011). I tried the following:
<?php
$time = new DateTime();
// works as expected, the first day of the current month
$time->modify('first day of this month');
echo $time->format('c')."\n";
// this doesn't work. I also tried several other ways
$time->modify('first day of january');
echo $time->format('c')."\n";
>
I know there are other ways to retrieve the date, but I search an string for DateTime->modify() no other solution.
You should specify the year too, as you can see in this example:
"first day of January 2008"
from the official doc.
Update: It works on php version >= 5.3.6
On v5.5.6
echo date('Y-m-d', strtotime('first day of January this year'));
Result: 2013-01-01
To get the day of the week for the first of the year
or the first day of the month
<?php
//This is for a given month
$m="May";
// this id for this month
//$m=date('F');
//if you want the day of Sunday instead D use lower case l
echo date('D', strtotime('first day of January this year'));
echo "<br>". date("D", strtotime('first day of'. $m ));
?>
Result Wed For May with D
Result Wednesday with l
This work for me (PHP 5.6 - not tested on older version)... as we talk for DateTime object
//Get current datetime
$now = new DateTime();
$now->modify('first day of January this year');
echo $now->format('Y-m-d');
// Print (current year)-01-01
echo (new DateTime())->modify('first day of January this year')->format('Y-m-d');
I'm trying to get the unix time for date strings that are formatted like so:
'second sunday of march 2010'
'first sunday of november 2010'
I was under the impression that strtotime could handle such a string, but apparently not, as this returns false. How can I convert to unix time when given a day of week, which one of those in the month (ie. first, second, etc.), a month and a year.
This should be possible with strtotime. You could try generating a timestamp of the first day of march using mktime() and adding that as a 2nd parameter (leaving just "first sunday" in the string part):
$timestamp = mktime (0,0,0,3,1,2010); // 1st of march
$first_sunday = strtotime("first sunday", $timestamp);
Not sure how this will handle the first day (March 1st) actually being a sunday. Make sure you check that out.
Also, and maybe this more reliable, check this out - the poster says he got good results with the following notation (quoting):
<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>
As always with strtotime, whatever you pick, make sure you test well, especially the edge cases (1st day of month is a sunday, last day of last month was a sunday....)
Your code works for me on PHP 5.3.0. What version of PHP are you using?
<?php
date_default_timezone_set("Europe/Oslo");
$time_march = strtotime('second sunday of march 2010');
$time_november = strtotime('first sunday of november 2010');
echo date("Y-m-d", $time_march) . " (timestamp: $time_march)\n";
echo date("Y-m-d", $time_november) . " (timestamp: $time_november)\n";
?>
gives:
2010-03-14 (timestamp: 1268521200)
2010-11-07 (timestamp: 1289084400)