Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
There appears to be a lot of info converting a time period into a time, but not the other way around.
An example of what I need to do, is convert say 120 minutes into P0DT2H0M0S.
And 13:10 into P0DT13H10M0S.
And 120 minutes into PT2H0M0S.
Any quick and easy way to do this?
I believe the format you're describing is the ISO 8601 date/time format. Here's how it describes intervals.
In the PHP documentation for the DateInterval class, someone has shared an example of how you might convert a string into ISO 8601 in an object-oriented way:
http://www.php.net/manual/en/class.dateinterval.php#113415
And here is someone else's solution, using functional rather than object-oriented date methods:
https://stackoverflow.com/a/13301472/2119660
The easiest way, to get ISO-8601 time interval duration, is with method createFromDateString
Use:
echo getISO8601duration_withZeros("3 year 20 day 15 minute"); # P3Y0M20DT0H15M0S
echo getISO8601duration_withZeros("1 hour 120 minutes"); # P0Y0M0DT1H120M0S
echo getISO8601duration_withZeros("7 year 5 month 3 day"); # P7Y5M3DT0H0M0S
# 13:10 example
$dt = new DateTime('13:10');
$interval_spec = "{$dt->format('H')} hour {$dt->format('i')} minute";
echo getISO8601duration_withZeros($interval_spec); # P0Y0M0DT13H10M0S
Function:
function getISO8601duration_withZeros($interval_spec) {
$interval = DateInterval::createFromDateString($interval_spec);
return $interval->format('P%yY%mM%dDT%hH%iM%sS');
}
Demonstration:
examples with zeros (code above);
examples without zeros;
examples with zeros, with recalculated carry over points;
examples without zeros, with recalculated carry over points.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have the following simple PHP code:
$timeRange = new DatePeriod(
new DateTime('09:00:00'),
new DateInterval('PT30M'),
new DateTime('18:00:00')
);
foreach ($timeRange as $time) {
$options[$time->format('H:m')] = $time->format('H:m');
}
I am expecting to get a list of times out of this that starts at 09:00, 09:30, 10:00, 10:30... and every half hour to ...16:30, 17:00, 17:30, 18:00.
What I'm actually getting is this list of times:
09:10, 10:10, 11:10, 12:10, 13:10, 14:10, 15:10, 16:10, 17:10
As you can see, this is wrong on two counts: it is incrementing by an hour each time rather than half an hour, and it is starting at an arbitrary time ten minutes past the hour.
Both of these are consistent; it isn't based on the time I ran the code, for example.
I also tried adding a hard-coded date to the DateTime classes, so that they looked like this:
new DateTime('2018-01-01 09:00:00')
This changed the output such that all the results were one minute past the hour. Again, it incremented by an hour each time.
I can't explain this behaviour. It seems to be completely wrong. Can anyone explain it, and tell me what I need to do to fix this.
I can, of course, revert to using timestamps; I have code for this that works already. But I would like to know what is going on with the DatePeriod.
For reference, this is running under PHP 7.2.4.
$options[$time->format('H:m')] = $time->format('H:m');
m is the month identifier. You want i:
$options[$time->format('H:i')] = $time->format('H:i');
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I tried the top answer of this question adding 1 day to a DATETIME format value but it didnt work. Im trying to get an easy solution for this: eg: 2017-12-31 +5 day. And I'd like to make a function where i give the current date and the days, or months etc, or if theres a built-in php that can do this, thats eaven better.
$date = "2017-12-31";
echo date("Y-m-d", strtotime($date . " +5 days"));
Strtotime() can translate some sentences to actions. See example 1
here is a alternative function:
function get5daysLater($date, $extra_days = 5)
{
echo date('Y-m-d', strtotime($date) +$extra_days *24*60*60);
}
Example of use:
echo get5daysLater("2017-12-07");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given today's date, is there an elegant way to get the previous Sunday's and the next Saturday's date?
So, given today's date of 12/12/2013, I would want to get back 12/8/2013 for the Sunday and 12/14/2013 for the Saturday.
If given a Sunday, for example 12/15/2013, I would want to get back 12/15/2013 for the Sunday and 12/21/2013 for the Saturday.
If given a Saturday, for example 12/21/2013, I would want to get back 12/15/2013 for the Sunday and 12/21/2013 for the Saturday.
And so on.
Any ideas?
DateTime() accepts relative formats:
echo (new DateTime('last Saturday'))->format("Y-m-d");
echo (new DateTime('next Saturday'))->format("Y-m-d");
See it in action
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string which is saved into a database on form process.
I want to create a script which scans the string for any potential dates.
For example:
"The client told us to ring him back this Saturday" "The patient has
an appointment scheduled for 10/10/2013" "Call him back at the end of
the month"
If any of these cases are found, a prompt should appear asking if we would like to create a reminder for that specific date.
PHP has a strtotime function, that can read dates like "this Saturday", and the usual 4/5/2014. You can use a regular expression, like this, to pull out the simple dates:
\d+\/\d+\/\d+ # e.g., 10/10/2012
And relative dates, with something like this.
(this|next|last|on) (Sat|Sun|Mon|Tues|Wedns|Thurs|Fri)day
Then another for months, and "tomorrow", and anything else you like. It'll get pretty complicated, but you can join them all together like (regexp1|regexp2|regexp3).
The great thing about strtotime is that it will just return false if it doesn't understand, so you don't need to be too exact.
If these aren't necessarily being processed the same day, you can also pass a timestamp from when the original entry was created.
Some things like "at the end of the month" you're going to have to do manually. strtotime reads a lot, but it's not that detailed.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to get the day after the easter date:
date("d-m-y", strtotime(easter_date(), '+1 day'));
I don't understand, the date showed is still the same, but when I try this:
date("d-m-y", strtotime('+1 day'));
It works.
How can I do?
That's because easter_date() produces a unix timestamp:
1364713200
And strtotime() is meant for this like 'today' and 'tuesday next week';
If you just want to add a day to it, just add 86400 (this is how many seconds are in a day) to easter_date();
So: date("d-m-y", easter_date() + 86400);
The function easter_date() returns a UNIX timestamp (e.g. 123338898) whereas strtotime()'s first parameter must be a valid date/time string (e.g. 2013-05-30 23:59:59). Valid formats are explained in Date and Time Formats.
Additionally, from the PHP documentation for strtotime():
Using this function for mathematical operations is not advisable. It
is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and
later, or DateTime::modify() in PHP 5.2.