DateInterval: Unknown or bad format - php

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?

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...

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.

formatting datetime from mysql in php

I am trying to format 2014-03-27 00:53:31 to be: 03/27/2014 I have tried many solutions but none of which have worked. My most recent is explained in this question.
So the time is stored in database as a datetime like: 2014-03-27 00:53:31
I call this by $customer->last_login;
Then I am trying to format this by doing the following:
$dt = $customer->last_login;
echo $dt->format('m/d/Y');
When I run this I get the following error:
Fatal error: Call to a member function format() on a non-object
What am I doing wrong? Or what is a better solution to formatting this to display just the date?
If $customer->last_login is a string, you have to convert to a DateTime object first.
$dt = new DateTime($customer->last_login);
echo $dt->format('m/d/Y');
Try this
echo date('m/d/Y',strtotime($dt));

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?

Contruct a datetime with specific format

i have a date string from my db, the date is 16/11/2010 and its format is d/m/Y, i want to modify its like this.
<?php
$date_from_db= '16/11/2010'; // format is d/m/Y
$date = new DateTime($date_from_db);
$date-> modify('+1 week');
echo $date-> format('d/m/Y') ;
?>
i have got this error
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (16/11/2012) at position 0 (1).
How can i fix this?
try
$date = DateTime::CreateFromFormat("d-m-y", "16-11-2010");
Unfortunately that is not one of the time formats supported by DateTime class constructor.
This page shows valid formats
http://www.php.net/manual/en/datetime.formats.date.php
You should use an actual SQL-compliant date field (typically YYYY-MM-DD) in your DB. Or if you can't change the way your are storing the dates, use DateTime::CreateFromFormat as suggested in other answer.
I would highly suggest using a more standard storage format though.

Categories