PHP's DatePeriod class giving inaccurate results [closed] - php

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');

Related

variable conditions from mysql in switch case on php/mysql? [closed]

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
my problem is that i have to implement variable conditions which means that the user is able to define a VALUE for period of time.This data will be stored in mysql. PHP is used on server-side.
i.e.
FROM 2018-01-01 TO 2018-01-03 VALUE 10;
FROM 2018-01-04 TO 2018-01-06 VALUE 20;
DEFAULT (used if not in time gap) VALUE 100;
Therefore I thought a switch case can be an option. But is it possible to have a foreach loop on cases?
like:
$date = "2018-01-01";
switch ($date) {
foreach(... as $data){
case $data:
//load variable
break;
}
default:
//load default values
}
Maybe i'm on the wrong way - pls hlp.
No, your syntax is wrong. In all programming languages that I can think of, you are not allowed to overlap blocks. A switch block can be contained in a loop, or one or more of it's cases' blocks may contain their own loops. (switch is a special case, in that may not have code outside it's case statements.)
You're (in a sense) trying to terminate a for-loop block, inside the block of an if statement within it.
The problem with your code, is that it will be unmaintainable, difficult to understand, and slightly inefficient.
The easiest thing to do, would be to run a cron job or something else which would be periodical and check if today's date falls in the date region defined by your user in the mysql data.
You could simply do with a better SQL statement to grab the data and store it in a slightly different way - for example:
start_date
end_date
and then your SQL result would do all the work of your current switch statement (the switch statement won't work as expected anyway).

PHP If statement with two timestamps [closed]

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 8 years ago.
Improve this question
I am trying to run a PHP if statement that says if there are more than 3600 seconds between two MySQL timestamps than do something. I think I have the IF statement written correctly but something is going on where it is always being resolved to true.
$time1=mysqli_query($con,"SELECT time_to_sec(max(time)) as time FROM table");
$time2=mysqli_query($con,"SELECT time_to_sec(max(time)) as time FROM table and time <>(SELECT max(time) FROM table)");
if (($time1->fetch_object()->time) - ($time2->fetch_object()->time) > 3600);
{
echo $test
}
When I run:
echo $time1->fetch_object()->time-$time2->fetch_object()->time;
It shows the correct value that I would expect but even if its less than 3600 it still does the echo $test. Any ideas what can be causing this?
Thank you!!!
Use CASE statements, a better and efficient approach:
Select event, case when TIMESTAMPDIFF(SECOND, last_timstamp, first_timestamp)<3600 then `under_time` when TIMESTAMPDIFF(last_timstamp, first_timestamp)>3600 then `over_time` end case as `duration_flag` from your_table where your_conditions
Then in your PHP you condition the output based on the queried flag.
This way you make less requests to the server, and has a cleaner query.
Maybe mysql has an old bug with TIMESTAMPDIFF, so if it doesn`t work, use this instead:
Time_To_Sec(last_timstamp) - Time_To_Sec(first_timestamp)

PHP convert time into a Time Period [closed]

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.

Scan long string for any potential dates [closed]

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.

How can I display this rather unusual schedule is an easy to digest manner via PHP and a web language? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'll do the best to explain this, but please ask clarifying questions as needed:
I basically have a schedule attempting to map out seven stages of an event, which occurs over a period of roughly a week. This data is presently in timestamps which are difficult to read and hard to draw conclusions from, and I would like to present it visually some how.
Each stage has an estimated period of time in which it might occur, and these times can overlap with other stages because they are estimates (and in fact typically will).
In addition to the estimates, in certain stages I will sometimes know when part of the stage is known, with certainty, to be occurring.
The purpose of this map/schedule is to allow a user to look at the schedule and instantly recognize what periods of time stages are likely to occur, and what periods of time stages are definitely occurring.
How do I translate my timestamps into some sort of visual to convey this information? Would HTML 5 be the way to go, and if so, in what manner?
Thank you for your input
The question is hard to answer - so I will only try the part where you are asking on how to visualize the information.
I can recommend using the highchart library for any kind of statistical visualization.
http://www.highcharts.com/
You can find some demos here:
http://www.highcharts.com/demo/
It all works with Javascript and even in older browsers which is nice.

Categories