I found a link earlier regarding using time diffs and getting the difference in minutes, hours and days:
How to get time difference in minutes in PHP
I was trying this:
$date1 = new DateTime('first day of this month', new DateTimeZone('Europe/Amsterdam'));
$date2 = new DateTime('first day of this month', new DateTimeZone('Europe/London'));
print_r($date1->format('Y-m-d H:i:s'));
print_r($date2->format('Y-m-d H:i:s'));
The output was like:
2013-12-01 13:00:36
2013-12-01 12:00:36
Then used this:
$diff = $date2->diff($date1);
print_r($diff);
But then i get 0 in all the differences. I want to get the difference between the two without using strtotime.. I is it outputing 0?
Your expectation doesn't make sense, since there is no difference. 2013-12-01 13:00:36 Amsterdam and 2013-12-01 12:00:36 London are the exact same point in time in human history. What you appear to expect is the offset difference between the London and Amsterdam timezones (i.e. GMT and GMT+1 differ by 1), but that has nothing to do with concrete timestamps.
You want to calculate the offset.Use DateTimeZone::getOffset()
$dateTimeZoneAmsterdam = new DateTimeZone("Europe/Amsterdam");
$dateTimeZoneLondon = new DateTimeZone("Europe/London");
$dateTimeAmsterdam = new DateTime('first day of this month', $dateTimeZoneAmsterdam);
$dateTimeLondon = new DateTime('first day of this month', $dateTimeZoneLondon);
$timeOffset = $dateTimeZoneAmsterdam->getOffset($dateTimeLondon);
print_r($timeOffset); // 3600
You are close. Try DateTime::diff
Related
I would like to find the date stamp of monday, tuesday, wednesday, etc. If that day hasn't come this week yet, I would like the date to be this week, else, next week. Thanks!
See strtotime()
strtotime('next tuesday');
You could probably find out if you have gone past that day by looking at the week number:
$nextTuesday = strtotime('next tuesday');
$weekNo = date('W');
$weekNoNextTuesday = date('W', $nextTuesday);
if ($weekNoNextTuesday != $weekNo) {
//past tuesday
}
I know it's a bit of a late answer but I would like to add my answer for future references.
// Create a new DateTime object
$date = new DateTime();
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
The nice thing is that you can also do this with dates other than today:
// Create a new DateTime object
$date = new DateTime('2006-05-20');
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
To make the range:
$monday = new DateTime('monday');
// clone start date
$endDate = clone $monday;
// Add 7 days to start date
$endDate->modify('+7 days');
// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');
$dateRange = new DatePeriod($monday, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo $day->format('Y-m-d')."<br />";
}
References
PHP Manual - DateTime
PHP Manual - DateInterval
PHP Manual - DatePeriod
PHP Manual - clone
The question is tagged "php" so as Tom said, the way to do that would look like this:
date('Y-m-d', strtotime('next tuesday'));
For some reason, strtotime('next friday') display the Friday date of the current week. Try this instead:
//Current date 2020-02-03
$fridayNextWeek = date('Y-m-d', strtotime('friday next week'); //Outputs 2020-02-14
$nextFriday = date('Y-m-d', strtotime('next friday'); //Outputs 2020-02-07
You can use Carbon library.
Example: Next week friday
Carbon::parse("friday next week");
PHP 7.1:
$next_date = new DateTime('next Thursday');
$stamp = $next_date->getTimestamp();
PHP manual getTimestamp()
Sorry, I didn't notice the PHP tag - however someone else might be interested in a VB solution:
Module Module1
Sub Main()
Dim d As Date = Now
Dim nextFriday As Date = DateAdd(DateInterval.Weekday, DayOfWeek.Friday - d.DayOfWeek(), Now)
Console.WriteLine("next friday is " & nextFriday)
Console.ReadLine()
End Sub
End Module
If I understand you correctly, you want the dates of the next 7 days?
You could do the following:
for ($i = 0; $i < 7; $i++)
echo date('d/m/y', time() + 86400 * $i);
Check the documentation for the date function for the format you want it in.
if you want to find Monday then 'dayOfWeek' is 1 if it is Tuesday it will be 2 and so on.
var date=new Date();
getNextDayOfWeek(date, 2);
// this is for finding next tuesday
function getNextDayOfWeek(date, dayOfWeek) {
// Code to check that date and dayOfWeek are valid left as an exercise ;)
var resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
}
Hope this will be helpfull to you, thank you
The PHP documentation for time() shows an example of how you can get a date one week out. You can modify this to instead go into a loop that iterates a maximum of 7 times, get the timestamp each time, get the corresponding date, and from that get the day of the week.
I have a date 2015-12-16
i want to get the date of the first day's date for the current week of my date
here 2015-12-16 is in the week 51 of the year then i want to get the first day's date of the week 51 ( 2015-12-14 here)
how could i do it ?
thank you
EDIT: it must work when there are 53 weeks in the year (like in 2015 for example)
You can do the following:
$dateTime = new DateTime("2015-12-16");
$weekNo = $dateTime->format("W");
$newDate = new DateTime();
$newDate->setISODate($dateTime->format("Y"), $weekNo);
Example:
http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65
Since the above is a bit off in some cases here's something more reliable:
$dateTime = new DateTime("2016-01-01");
$dateTime->sub(new DateInterval("P".($dateTime->format("w")-1)."D")); //Since the weekdays are 1-based.
Example:
http://sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851
$date = new \DateTime('2015-12-16');
echo $date->modify('last sunday +1 day')->format('Y-m-d');
This gets start of this week if you count monday to sunday.
Try this:
date('Y-m-d', strtotime('This week', strtotime('2015-12-16')));
If I send a DAY and TIME (not DATE) to the DateTime API like this :
$tz = new \DateTimeZone("UTC");
$now = new \DateTime("now", $tz);
$then = \DateTime::createFromFormat('l g A', 'Thursday 8 PM', $tz);
And it is currently Saturday 26th March, $then, when echoed as follows :
echo $then->format('l jS, F');
Will return :
Thursday 31st, March
How do I make it return the date of the Thursday in the CURRENT week that has just passed :
Thursday 24th March
Not the next Thursday?
DateTime::__construct() and strtotime() uses a lot of rules to guess what you mean when you specify an incomplete or relative date. Most of the times thy guess right but some of the rules are very similar and the final result is not the one expected by the programmer.
DateTime::createFromFormat() is limited, it cannot understand all the formats from the list.
You try to parse a relative date format. When only the day name is specified, it is interpreted as the next occurrence of the specified day of the week (see the dayname entry under the Day-based Notations table in the documentation.)
You can get the date you want by using Thursday this week instead. DateTime::createFromFormat() doesn't understand relative dates that contain references to the week but DateTime::__construct() does.
Try this:
$then = new \DateTime('Thursday this week 8 PM', $tz);
Could always call the 'sub' method on it, subtracting 7 days.
$tz = new \DateTimeZone("UTC");
$now = new \DateTime("now", $tz);
$then = \DateTime::createFromFormat('l g A', 'Thursday 8 PM', $tz);
$then->sub(new DateInterval('P7D'));
echo $then->format('l jS, F');
I have an issue with converting DateTime from Moscow timezone to New York timezone. Here is my test script:
$year = 2015;
$month = 3;
$tzMoscow = new DateTimeZone('Europe/Moscow');
$tzNewYork = new DateTimeZone('America/New_York');
$startDate = DateTime::createFromFormat('Y-n-d', "$year-$month-01", $tzMoscow);
echo $startDate->format('Y-m-d H:i:s')."\n"; // 2015-03-01 16:16:05
$startDate = DateTime::createFromFormat('Y-n-d', "$year-$month-01", $tzNewYork);
echo $startDate->format('Y-m-d H:i:s') . "\n"; // 2015-03-01 09:16:05
$startDate->setTimezone($tzMoscow);
echo $startDate->format('Y-m-d H:i:s') . "\n"; // 2015-03-01 17:16:05
Third output is incorrect, time should be 16:16:05. Am I doing something wrong or is this a bug in php?
I think I've got it. The problem is that you're not specifying a time of day, so createFromFormat is using "the current system time":
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
Now what does it mean to use "the current system time" to create a time in the New York time zone, when that time zone has changed UTC offset between the specified date (March 1st) and the current date (March 14th)? It was UTC-5 on March 1st, and it's now UTC-4 due to daylight saving time.
I believe PHP is taking the current time of day in the specified time zone (9:16 at the time you ran that code) and then using that as the time of day on the specified date. So we end up with March 1st 2015, 09:16 New York time - or March 1st, 14:16 UTC, which is indeed March 1st 17:16 Moscow time.
That's not the same as the current time of day in Moscow at the time that you ran the code, which was 16:16.
Basically, you should try not to do this - or expect problems like this to happen. Think about what time of day you're really trying to represent, bearing in mind that within a particular time zone, offsets change over time. I can't really advise you on what your code should be, because we don't know what you're trying to achieve - but using the current time of day for a different date can definitely cause this problem.
I also believe this is an issue of DST, changing in New York time zone on 8th of March. I extended your date formats to Y-m-d H:i:s U I to include the unix timestamp and the DST value.
The output is now as following:
2015-03-01 19:07:17 1425222437 0
2015-03-01 11:07:17 1425226037 0
2015-03-01 20:07:17 1425226037 0
As you can see, the unix timestamp is already different between both created DateTime objects.
Now when I specify a concrete time as
$startDate = DateTime::createFromFormat('Y-n-d H:i', "$year-$month-01 00:00", $tzMoscow);
as well as
$startDate = DateTime::createFromFormat('Y-n-d H:i', "$year-$month-01 00:00", $tzNewYork);
the output changes to:
2015-03-01 00:00:00 1425153600 0
2015-03-01 00:00:00 1425186000 0
2015-03-01 09:00:00 1425186000 0
On the other hand, if I change $month to 4 (where New York uses DST), I get the following output:
2015-04-01 19:08:35 1427900915 0
2015-04-01 11:08:35 1427900915 1
2015-04-01 19:08:35 1427900915 0
What do these results mean?
The conversion between New York and Moscow time zone works correct in all cases, as you can judge from the unix timestamp being identical. Also, "2015-03-01 00:00" are obviously different timestamps for Moscow and New York, because they depend on the concrete time zone.
So I think your code is correct and there is no bug in php. However, the "current" times in New York and Moscow differ due to the DST switch between 1st of March and today (14th of March).
So while Jon's answer already explained the theory (I don't want all the credit, he was first), maybe someone will still find some concrete examples useful.
You can use this function, for change between timezones
function changeTimezone($time, $currentTimezone, $timezoneRequired, $FormtsTime = 'Y-m-d h:i:s')
{
$dayLightFlag = false;
$dayLgtSecCurrent = $dayLgtSecReq = 0;
$system_timezone = date_default_timezone_get();
$local_timezone = $currentTimezone;
date_default_timezone_set($local_timezone);
$local = date($FormtsTime);
date_default_timezone_set("GMT");
$gmt = date($FormtsTime);
$require_timezone = $timezoneRequired;
date_default_timezone_set($require_timezone);
$required = date($FormtsTime);
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
$date = new DateTime($time);
$date->modify("+$diff1 seconds");
$date->modify("+$diff2 seconds");
if ($dayLightFlag) {
$final_diff = $dayLgtSecCurrent + $dayLgtSecReq;
$date->modify("$final_diff seconds");
}
$timestamp = $date->format($FormtsTime);
return $timestamp;
}
I would like to find the date stamp of monday, tuesday, wednesday, etc. If that day hasn't come this week yet, I would like the date to be this week, else, next week. Thanks!
See strtotime()
strtotime('next tuesday');
You could probably find out if you have gone past that day by looking at the week number:
$nextTuesday = strtotime('next tuesday');
$weekNo = date('W');
$weekNoNextTuesday = date('W', $nextTuesday);
if ($weekNoNextTuesday != $weekNo) {
//past tuesday
}
I know it's a bit of a late answer but I would like to add my answer for future references.
// Create a new DateTime object
$date = new DateTime();
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
The nice thing is that you can also do this with dates other than today:
// Create a new DateTime object
$date = new DateTime('2006-05-20');
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
To make the range:
$monday = new DateTime('monday');
// clone start date
$endDate = clone $monday;
// Add 7 days to start date
$endDate->modify('+7 days');
// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');
$dateRange = new DatePeriod($monday, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo $day->format('Y-m-d')."<br />";
}
References
PHP Manual - DateTime
PHP Manual - DateInterval
PHP Manual - DatePeriod
PHP Manual - clone
The question is tagged "php" so as Tom said, the way to do that would look like this:
date('Y-m-d', strtotime('next tuesday'));
For some reason, strtotime('next friday') display the Friday date of the current week. Try this instead:
//Current date 2020-02-03
$fridayNextWeek = date('Y-m-d', strtotime('friday next week'); //Outputs 2020-02-14
$nextFriday = date('Y-m-d', strtotime('next friday'); //Outputs 2020-02-07
You can use Carbon library.
Example: Next week friday
Carbon::parse("friday next week");
PHP 7.1:
$next_date = new DateTime('next Thursday');
$stamp = $next_date->getTimestamp();
PHP manual getTimestamp()
Sorry, I didn't notice the PHP tag - however someone else might be interested in a VB solution:
Module Module1
Sub Main()
Dim d As Date = Now
Dim nextFriday As Date = DateAdd(DateInterval.Weekday, DayOfWeek.Friday - d.DayOfWeek(), Now)
Console.WriteLine("next friday is " & nextFriday)
Console.ReadLine()
End Sub
End Module
If I understand you correctly, you want the dates of the next 7 days?
You could do the following:
for ($i = 0; $i < 7; $i++)
echo date('d/m/y', time() + 86400 * $i);
Check the documentation for the date function for the format you want it in.
if you want to find Monday then 'dayOfWeek' is 1 if it is Tuesday it will be 2 and so on.
var date=new Date();
getNextDayOfWeek(date, 2);
// this is for finding next tuesday
function getNextDayOfWeek(date, dayOfWeek) {
// Code to check that date and dayOfWeek are valid left as an exercise ;)
var resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
}
Hope this will be helpfull to you, thank you
The PHP documentation for time() shows an example of how you can get a date one week out. You can modify this to instead go into a loop that iterates a maximum of 7 times, get the timestamp each time, get the corresponding date, and from that get the day of the week.