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

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?

Related

mogodb aggregate works on playground but not in php

I got that script on play ground that works perfect
PlayGround.
in php I got error that from my understanding its the conversation of the date to string
what can be the reason?
can i use other method instead that will work?
I'm storing date as Y-m-d H:i:s in mongodb.
the error is when subtracting $answerTime - $startTime
the error
Fatal error: Uncaught MongoDB\Driver\Exception\CommandException: Error parsing date string ''; 0: Empty string ' in...

Error in php code when changing plesk server

I have this code:
$ah = date("H:i:s Y-m-d");
$ahseg=mktime($ah);
and this error:
Fatal error: Uncaught TypeError: mktime(): Argument #1 ($hour) must be of type int, string given in /var/www/vhosts/dominio.com/httpdocs/admin/maniobra.php:8 Stack trace: #0 /var/www/vhosts/dminio.com/httpdocs/admin/maniobra.php(8): mktime() #1 {main} thrown in /var/www/vhosts/dominio.com/httpdocs/admin/maniobra.php on line 8
This code was working on other hosting, but when I pass it to the new server that uses plesk it throws me this error
Because of a php version change:
see: https://3v4l.org/she2g
If you want to have a timestamp, use time(), if you want to use mktime(), check the dox, you are giving a string date to the hour (int) parameter:
https://www.php.net/manual/en/function.mktime.php
Regardless of whether it used to work, the error message is quite straight forward: the first argument is supposed to be an int, but you are passing a string.
The documentation for mktime confirms that while it's possible to call it with one argument, that one argument is the hour, not a formatted date time string.
The only reason this used to work is that in earlier PHP versions, the string was "coerced" to an integer, so you were running this:
$ah = date("H:i:s Y-m-d");
$ahseg=mktime((int)$ah);
If the string in $ah is "10:59:00 2022-02-02", then the integer passed to mktime is 10, which happens to be the hour. The rest of the string is thrown away, and other fields are left at their default values ("now").
It's unclear what this code was trying to do, but for an integer representing "now", you can just use time(); for an integer representing some other date, use something like strtotime('2020-01-01 19:45');.

Carbon date bug on php 7.0

I'm having a problem with composer package illuminate/database and Carbon on PHP 7.0.24.
Sometimes php is producing the following exception
Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (2018-03-09 16:56:16.-51861) at position 24 (6): Unexpected character in /volume1/web/octopus/vendor/nesbot/carbon/src/Carbon/Carbon.php:413
Stack trace:
#0 /volume1/web/octopus/vendor/nesbot/carbon/src/Carbon/Carbon.php(413): DateTime->__construct('2018-03-09 16:5...', Object(DateTimeZone))
1 /volume1/web/octopus/global_bootstrap.php(48): Carbon\Carbon->__construct()
2 /volume1/web/octopus/app/bootstrap.php(8): require_once('/volume1/web/oc...')
3 /volume1/web/octopus/app/dashboard.php(8): require('/volume1/web/oc...')
4 {main}
thrown in /volume1/web/octopus/vendor/nesbot/carbon/src/Carbon/Carbon.php on line 413
if I only create an instance of Carbon without any parameters like this
<?php
// ...composer initialization...
$carbon = new \Carbon\Carbon();
var_dump($carbon);
After looking deeper into Carbon-class there is an if clause for php versions lower than 7.1 which adds microtime behind a string formatted date, but why?
Is there a bug in carbon for php versions lower than 7.1?
I solved my problem by adding two lines at the top of my php script:
<?php
date_default_timezone_set('Europe/Berlin');
setlocale(LC_ALL, 'de_DE#euro', 'de_DE', 'deu_deu');
// ....
$carbon = new \Carbon\Carbon();
var_dump($carbon);

DateTime Uncaught exception DateTime::__construct()

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.

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