I am taking in a parameter that is a date in PST timezone which will be in the format of "YYYY-MM-DD" (e.g. "2011-08-15"). This parameter is optional. I have 2 questions that I've been struggling with.
I need to calculate the start and end datetime in UTC for this date.
So if the inputted date is 2011-08-15, I want to get the start and end datetimes:
2011-08-15 07:00:00
2011-08-15 06:59:59
(These are essentially the beginning and end of day)
Second, is to handle the case when the date is not passed in. I want to default to the current PST date them and start from there. So if the current datetime is 2011-08-01 10:00:00, I want to get the same start and end datetimes similar to the first scenario except it's based on the inputted date.
2011-08-01 07:00:00
2011-08-01 06:59:59
I've been pulling my hair out dealing with date and datetime conversions. I'm sure I'm missing something super straightforward.
Parse the date and assume PST timezone:
$date = new DateTime("2011-08-15", new DateTimeZone("PST"));
Change the timezone to UTC: (this does the all conversions for you)
$date->setTimeZone(new DateTimeZone("UTC"));
Calculate start and end. Start is our $date and end is $date + 1 day
$start = $date;
$end = clone $date;
$end->modify("+1 day"); // now $end is $start + 1 day
Print start/end:
printf("start: %s, end: %s\n", $start->format('Y-m-d H:i:s'), $end->format('Y-m-d Hi:s'));
// this prints start: 2011-08-15 07:00:00, end: 2011-08-16 07:00:00
For the last part of your questions, you can easily compare two dates:
if ($date > new DateTime()) { // if $date is after now
// do something
}
So you could do something like that:
if ($date > new DateTime()) {
$date->setTimeZone(new DateTimeZone("UTC"));
}
If you don't much like the OO syntax you could also use the function aliases:
$date = date_create(...);
date_format($date, ...);
date_modify($date, ...);
// ...
Use setTimezone function
Working example (I'm not in PST timezone, so I have to set it explicitly)
$date_input = "20011-09-15";
//$date_input = null; //That will emulate no-input case
date_default_timezone_set("America/Los_Angeles"); //if you are in PST, you don't need this line
$date_start = new DateTime($date_input);
$date_end = new DateTime($date_input);
$date_end->modify("+1 day");
/*$date_start->setTimezone(new DateTimeZone("America/Los_Angeles"));
$date_end->setTimezone(new DateTimeZone("America/Los_Angeles"));*/
//end of date is equal to start of the next day.
//But, if you need something like 2011-08-11 23:59:59 add $date_end->modify('-1 second')
$date_start->setTime(0,0,0);
$date_end->setTime(0,0,0);
echo "Date Start PST:".$date_start->format("Y-m-d H:i:s")."<br/>";
echo "Date End PST:".$date_end->format("Y-m-d H:i:s")."<br/>";
//UTC is equal to London time. Almost :)
$date_start->setTimezone(new DateTimeZone ('Europe/London'));
$date_end->setTimezone(new DateTimeZone ('Europe/London'));
echo "Date Start UTC:".$date_start->format("Y-m-d H:i:s")."<br/>";
echo "Date End UTC:".$date_end->format("Y-m-d H:i:s")."<br/>";
Related
I need to create an empty date with DateTime(). So, all zeros. The time should be displayed in that way: 0000:00:0:00:00.
What I have tried:
$date = new DateTime("2019-05-09 12:07");
$date->setTime(0, 0);
$date->setDate(0, 0, 0);
echo $date->format("Y:W:j:H:i");
That outputs
-0001:49:30:00:00
instead of 0000:00:0:00:00
What could I do to achieve a zero datetime?
More an explanation of what is going wrong than how to achieve what you are after - even if it is possible.
From a date point of view - 0 is invalid for both the month and day. In the manual it has the example
Example #2 Values exceeding ranges are added to their parent values
So if you have greater than the number of days in a month, it will make it the next month
In your case it is almost the opposite, having a number less than the start of the month. So 0 as the month and day, it will act as -1, so if you formatted it as
echo $date->format("Y:m:d:H:i");
the output is
-0001:11:30:00:00
So as you can see, the day and month are 0-1 (11 and 30) and this has overflowed into the year with -1.
No, You cannot make a datetime 0000:00:0:00:00 from DateTime()
php > echo (new DateTime("0000-00-00 00:00:00"))->format("Y-W-j H:i");
-0001-49-30 00:00
php > echo (new DateTime())->setISODate(0,0,0)->setTime(0,0,0,0)->format("Y-W-j H:i");
-0001-52-26 00:00
Surely Not..!
although you can create any Date with PHP helper function (that uses DateTime class behind) any of you desire date with Zeor Time only
$date = date_create('now'); // or any '1970-01-01'
$date = date_format($date, 'Y-m-d 00:00:00');
// or
$date = date('Y-m-d 00:00:00', strtotime('2021-12-21'));
// or
$date = date('Y-m-d 00:00:00');
output: "2021-12-21 00:00:00"
you can initialize the time with current time and take difference of the current time after. its a trick you can perfom to get (0000-00-00 00:00:00).
$currentTime = new DateTime('NOW');
$diff = $currentTime->diff(new DateTime('NOW'));
echo $diff->format("%Y-%m-%d %H %i %s");
that will give you 0000-00-00 00:00:00
is it possible to add a variable string like '2 day, 2 weeks or even 4 hours' to a date time in PHP.
For example:
I have a date time like this: '2017-08-02 12:00'
now the user choose an interval like '4 hours or 2 weeks'
now the user choice should be added to the date time.
Is this possible?
I don't want the whole code, maybe just an advice how to do that.
thanks
Yes, use
$userDate = strtotime('2017-08-02 12:00:00');
echo date('Y-m-d H:i:s', strtotime('+4 hours', $userDate));
to get date after 4 hours
Example
Explanation
strtotime converts about any English textual datetime description into a Unix timestamp. Most commonly it's used with actual date string or with difference string. E.g. +5 months, 'next Monday' and so on. It will return Unix timestamp - integer that represents how much seconds there is after 1970-01-01 (1970-01-01 00:00:00 is 0, 1970-01-01 00:01:00 is 60 and so on).
So in strtotime('2017-08-02 12:00:00') we convert date to integer for later use.
strtotime('+4 hours', $userDate) - here we use our date as "now" parameter (by default it's time()) and requesting to return timestamp after 4 hours.
echo date('Y-m-d H:i:s', ...); - date accepts format and Unix timestamp to convert from integer back to human readable text.
May be you are looking for this:
http://php.net/manual/en/datetime.modify.php
$date = new DateTime('2006-12-12');
$date->modify('+1 day');
echo $date->format('Y-m-d');
For a datetime you can use the add method but you have to put in the correct code for the amount to add.
$my_date = new DateTime();
$addition = 4;
$my_new_date = $my_date->add(new DateInterval("P${addition}D"));
echo $my_new_date->format('Y-m-d H:i:s');
Where addition is your variable that you want to add.
I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.
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;
}
Seems I don't quite understand much the function strtotime. My case is I would like to compare the current time (now) with a specific time on specific timezone
For example the specific time is "this Monday at 14:00:00" at the timezone "America/New_York":
$specificTime = strtotime("monday this week 14:00:00 America/New_York");
My current code is:
$now = strtotime("now America/New_York");
if ($now > $specificTime) {
//do something
}
But I have figured it out that $now above is 6 hours ahead with current time. The number 6 I guess from offset -05:00 of America/New_York, plus with 1 hour daylight saving.
it should remove timezone out of $now, it will work correctly:
$now = strtotime("now");
if ($now > $specificTime) {
//do something
}
Could someone give the explain why strtotime("now America/New_York") is 6 hours ahead with strtotime("now), why they are not equivalent? really confused.
P.S: I am on GMT+07:00.
Simple debugging:
<?php
$now = strtotime("now America/New_York");
echo date('r', $now);
// Thu, 28 Nov 2013 16:39:51 +0100
... shows that such command is doing this:
Calculate local time in my default time zone (10:39:51 +0100)
Return timestamp that corresponds to 10:39:51 in New York time (-0500)
Doing date manipulation with strings is terribly complicated. Just imagine you'd try to do math with string functions: strtofloat('one plus square root of half hundred')—there'd be plenty of room for mistakes. So my advise is to keep it simple and only use with simple expressions when there's some benefit, such as strtotime('+1 day').
If you need to work with different time zones, I suggest you use proper DateTime objects. If you choose to work with Unix timestamps, forget about time zones: Unix timestamps do not have time zone information at all.
You can use DateTime for this. I believe settings a timezone in strtotime is not valid.
$specificTime = new DateTime("monday this week 14:00:00", new DateTimeZone("America/New_York")));
$now = new DateTime("now", new DateTimeZone("America/New_York"));
You can then compare unix timestamp with this:
if ($now->getTimestamp() > $specificTime->getTimestamp()) {
// do something ...
}
There is time offset between each timezone.
strtotime() function will return the Unix timestamp according the timezone.
It will use the default time zone unless a time zone is specified in that parameter.
The default time zone it the return value of date_default_timezone_get();
Look the code below:
<?php
// UTC
echo date_default_timezone_get(), "\n";
// 2013-11-28 14:41:37
echo date('Y-m-d H:i:s', strtotime("now America/New_York")), "\n";
// 2013-11-28 09:41:37
echo date('Y-m-d H:i:s', strtotime("now")), "\n";