DateTime Uncaught exception DateTime::__construct() - php

Yesterday i was working on a website that we are gona use.
It worked good etc. However when i start it right now, i get a error...
We get information from a other website wich posts the date like this:
2016-30-24T11:30:00.000+01:00
However we only want to show:
11:30:00
Hours Minuts and Seconds.
We did it with the folowing method:
$start_time = new DateTime($meeting->start, new DateTimeZone('UTC'));
echo '<p><b>Start:</b> ' . date_format($start_time, 'H:i:s') . '</p>';
This was working yesterday, however now i get the following error:
Fatal error: Uncaught exception 'Exception' with message
'DateTime::__construct(): Failed to parse time string
(2016-21-24T08:21:00.000+01:00) at position 6 (1): Unexpected
character' in index.php:58 Stack trace: #0
index.php(58): DateTime->__construct('2016-21-24T08:2...',
Object(DateTimeZone)) #1 {main} thrown in
index.php on line 58
How do i solve this error, and why was it working yesterday and not now?

The exception is telling you that the date is nonsense, to which I'd agree.
Neither the 24th of the 21st month nor the 21st of the 24th month makes any sense.

Related

Conversion of seconds to duration working on one server, but not another

I'm trying to convert a number of seconds into days, hours and minutes. The code is below:
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes');
On my deployment server it works fine:
1 days, 4 hours, 27 minutes
On my development server (XAMPP), I get this error:
Fatal error: Uncaught Exception: Failed to parse time string (#) at position 0 (#): Unexpected character in D:\220909_XAMPP\htdocs\PC\PupController\worker.php:261 Stack trace: #0 D:\220909_XAMPP\htdocs\PC\PupController\worker.php(261): DateTime->__construct('#') #1 D:\220909_XAMPP\htdocs\PC\PupController\index.php(92): Pup->lastMonth() #2 {main} thrown in D:\220909_XAMPP\htdocs\PC\PupController\worker.php on line 261
Can someone help?
It turned out I had forgotten to change a setting on one copy!

DateInterval: Unknown or bad format

I get several dateTime informations through an API. It's format is like P29DT48M56.941999S for example. I would like to convert it with to a readable sting using DateInterval::format. My snippet looks like
$interval = new DateInterval('P29DT48M56.941999S');
echo $interval->format(' %d days, %I minutes');
Unfortunally, I get this error:
PHP Fatal error: Uncaught exception 'Exception' with message 'DateInterval::__construct(): Unknown or bad format (P29DT48M56.941999S)'
What's wrong and how can I fix it?

Getting Error from DateTime is there a bug in it in PHP

I'm getting the following error coming from Cron every time it runs my code and it looks like it is coming from DateTime is there a bug in DateTime or am I not using it correctly.
Line 24 in the error is this:-
$date = new DateTime($datetrue);
and what I am giving DateTime in $datetrue is 2014-06-13 13:00:00
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (2014-06-13 25:30:00) at position 11 (2): Unexpected character' in /home3/rdkurth/public_html/cron.php:24
Stack trace:
#0 /home3/rdkurth/public_html/cron.php(24): DateTime->__construct('2014-06-13 25:3...')
#1 {main}
thrown in /home3/rdkurth/public_html/cron.php on line 24`
My code:-
require_once("connect.php");
require_once ("function.php");
$sql = "Select
signings.id,
signings.pid,
signings.signstart,
signings.starttime,
remindertime.rtime,
signings.done
From
signings Inner Join
profile On profile.id = signings.pid Inner Join
remindertime On profile.emailrt = remindertime.rtid
Where
signings.pid = 7 And
signings.signstart = CurDate() And
signings.done = 0";
foreach ($db->query($sql) as $signings)
{
$signingid = $signings['id'];
$newsub= '- ' . $signings['rtime']; // the time that needs to subtracted from the scheduled date
$datetrue =$signings['signstart'] . ' ' . $signings['starttime']; //The schedule appontment time
$date = new DateTime($datetrue);
$date = date_modify($date, $newsub);
$senddate = date_format($date, 'Y-m-d H:i'); //the date to send the remider
if ($senddate == date('Y-m-d H:i')) {
//get all the customers data to put in the message
Try using strtotime() directly. My experience with the DateTime class is that is does not always behave as expected and documented. In Addition, if you look into php's documentation on the class there is a lot of change across different php versions.
My second advice is to define a common time zone for your application system. You are comparing data taken from a database with time values generated by your php application. Unless you set the time Zone explicitly you may run into issues at some point.
Refer to http://de2.php.net/manual/en/function.date-default-timezone-set.php
Third advice: Never do operations that may fail in construct statements. When using explicit (member) functions you get better error responses in most cases. In fact, exception handling should always be the last line of defence - do not consider your code to be complete unless you implemented procedural error handling!

Is it me or there's a bug in PHP DateTime?

I've just encountered on a problem I never though would exist.
I have a form that submits dates in the following format 04/28/2013 11:00. On the user front-end I'm using jquery datetimepicker and on the backend I have php to process the form.
Doing some testing I found out that DateTime in php does not throw an exception when the time is broken. For example this 04/28/2013 11:00123123 would not trigger an exception - instead DateTime returns now time. In my case the date is not related to now - it's a specific date & time in the future.
In my opinion DateTime should return an exception rather than now time. Is it me, or this is a bug?
Edit:
I'm using php 5.3.23
Posting this as an answer as the comments wouldn't fit for it.
<?php
new DateTime('04/28/2013 11:00123123');
I'm getting:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (04/28/2013 11:00123123) at position 16 (1): Double time specification' in ...
Exception: DateTime::__construct(): Failed to parse time string (04/28/2013 11:00123123) at position 16 (1): Double time specification in ...
Call Stack:
0.0001 635184 1. {main}()
0.0001 636048 2. DateTime->__construct()
I'm using PHP5.3.10. And you?

Error when trying to create an object DateTime in PHP

Today I got a project written in PHP. There's an error when I try to show a date in a table data set. This is the statement that generates the error:
$data = new DateTime($registro["previsao de entrega"];
The error message is this:
Fatal error: Uncaught exception 'Exception' with message
'DateTime::__construct() Failed to parse time string (2 dez 2011
16:00) at position 0 (2): Unexpected character' in
C:\www\fluxo_producao\Telas\TelaFluxoProducao.php:941 Stack trace: #0
C:\www\fluxo_producao\Telas\TelaFluxoProducao.php(941):
DateTime->__construct('2 dez 2011 16:0...') #1 {main} thrown in
C:\www\fluxo_producao\Telas\TelaFluxoProducao.php on line 941
What I discovered by myself was that if I change the parameter manually to "12 Dec 2006" for example, the function works. But the date the variable is passing is "12 Dez 2006" (Brazilian format, by the way, I'm Brazilian ^_^), and i found too that the default timezone in the server is "America/Sao_Paulo"... What do I have to change in the function or parameters to make it convert the format specified?
It needs to have a closing curly bracket on the end?
$data = new DateTime($registro["previsao de entrega"]);
If this is just a typo it could be the value of $registro["previsao de entrega"] is incorrect, what is it?
You can see allowed value formats here: http://www.php.net/manual/en/datetime.formats.date.php
It will only accept English month values, eg, Dec will work but Dez will not.
Verify that you have set your server to the correct time locale:
setlocale(LC_TIME, 'pt_BR');
This is required for strftime() and AFAIK all the date/time related functions and the DateTime class use the same library which will require/respect this setting.

Categories