I am trying to get the second thursday from a datetime.
Code
$test = new DateTime('2013-08-02 10:00');
echo $test->modify('second thursday'.$test->format('H:i'))->format('Y-m-d H:i');
The above code returns 2013-08-15 10:00 instead of 2013-08-08 10:00
But it works correctly when I use the following code
$test = new DateTime('2013-08-02 10:00');
echo $test->modify('second wednesday'.$test->format('H:i'))->format('Y-m-d H:i');
First day of month of 2013-August is Thursday.
And there is bug in the code that if if the first day is Thursday , first Thursday will come in the next week i.e 8-August and like wise 2nd second Thursday will come on 15-August
this code will help
echo date('Y-m-d', strtotime('second thursday of august 2013'));
updated code
$test = new DateTime('2013-08-02 10:00');
echo date('Y-m-d', strtotime("second thursday of august 2013")).' '.$test->format('H:i');
Added By RC
From PHP strtotime manual page
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given
weekday in a month where that weekday was the first day of the month
would incorrectly add one week to the returned timestamp. This has
been corrected in 5.2.7 and later versions.
try this,
echo date('Y-m-d', strtotime('second thursday of august 2013'));
try this one. i have already check and it shows the result you want.
$test = new DateTime('2013-08-00 10:00');
echo $test->modify('second thursday'.$test->format('H:i'))->format('Y-m-d H:i');
$test = new DateTime('2013-08-02 10:00');
echo $test->modify('second thursday'.$test->format('H:i'))->format('Y-m-d H:i');
'second Thursday' means the second Thursday after the current date. So the expected outcome is 2013-08-15. If you want the second Thursday of the given month, you could specify it like this:
$test = new DateTime('2013-08-02 10:00');
echo $test->modify('second thursday of this month '.$test->format('H:i'))->format('Y-m-d H:i');
Note that 'this month' refers to the date currently stored in the DateTime object, not on the date the code is executed.
Related
I want to display the first day of the month in 'day of the week' format.
For example, The below code should select the month of August and say the day of the week 01 falls on which is Thursday, but for some reason, the below code outputs Friday which is wrong, on some months such as May it correctly says Wednesday.
$monthNumber = 3;
$base = strtotime(date('Y-m',time()) . '-01 00:00:01');
$dateTest = date("Ym" . 1, strtotime($monthNumber . " month", $base));
$unixTimestamp = strtotime($dateTest, $base);
echo date("l", $unixTimestamp);
Does anyone have any ideas to make it show the correct day?
$base fixes a bug with it not showing the correct month.
And why won't you use the dateTime?
echo (new \DateTime('first day of august 2019'))->format('l');
Read more about Relative formats.
You have to use date() at first:
$base = date('Y-m',time()) . '-01 00:00:00';
And dont have to set the first second :)
To get the day of the week falling on the first day of any month you can use the prefix first day of e.g.
echo (new \DateTime('first day of August'))->format('l');
This will show correctly Thursday for August 2019.
If you want to do it dynamically using numbers for months, you can create a dummy date like you were trying to do.
$timeString = sprintf('01.%02d.%04d', 8, 2019); // replace the numbers with your variables
echo (new \DateTime($timeString))->format('l');
I can get this week Thursday by doing below
$startdate = (new DateTime())->setTime(0,0,0);
$startdate->setISODate($startdate->format("Y"), $startdate->format("W"), 4);
But if i try a relative date it get this week Thursday before or on Thursday and gets next Thursday after Thursday.
$startdate = (new DateTime('thursday'))->setTime(0,0,0);
I also tried "this thursday" relative date but it doesnt return what i would excpect, it acts like "thursday". I can use the setISODate but im am just curious if i can do with a relative date since it would be easier.
http://php.net/manual/en/datetime.formats.relative.php
Try this:
$startdate1 = (new DateTime('thursday last week'))->setTime(0,0,0); //2017-01-19
$startdate2 = (new DateTime('thursday this week'))->setTime(0,0,0); //2017-01-26
$startdate3 = (new DateTime('thursday next week'))->setTime(0,0,0); //2017-02-02
I have a script which gets the monday of the week to be able to display a calendar by using a while loop, so it gets the monday of that week then list out 7 divs which correspond to a day of the week.
I thought it was working but today(sunday) it has start listing the week starting tomorrow(monday) why is this? the code I have is:
$days = date("j",strtotime("monday this week"));
That variable returns 8, meaning PHP thinks the week starts on a sunday?
This should work for you:
echo $monday = date("j",strtotime('last monday', strtotime('tomorrow')));
Output:
1
You are better of relying on ISO8601 standard:
$dt = new DateTime();
$dt->setISODate($dt->format('o'), $dt->format('W'));
echo $dt->format('Y-m-j');
demo
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');
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');