I'm reading a CSV file with datetime fields but when I try to convert the date to Carbon by doing $date = new Carbon($row['date']) I get one of these errors:
DateTime::__construct(): Failed to parse time string (09/07/2014 16:55:22 MEST) at position 20 (M): The timezone could not be found in the database
DateTime::__construct(): Failed to parse time string (24/01/2014 16:57:27 MET) at position 0 (2): Unexpected character
Seems PHP does not recognize the MET/MEST timezones. How can I dynamically convert any non-standard timezones to create a Carbon/DateTime object?
The format may not be in a recognizable format. Use this instead. It will only work with MET, as MEST is not a valid timezone
$date = Carbon::createFromFormat('d/m/Y H:i:s T', $row['date']);
Related
I want to parse a string of the form 02.01.19 13:49 with Carbon.
Now \Carbon\Carbon::parse('02.01.19 13:49'); fails with
DateTime::__construct(): Failed to parse time string (20.01.19 13:49)
at position 9 (1): Double time specification
Is there any way to tell Carbon that the 19 represents 2019?
This is how I managed to do it, buts its a bit ugly:
\Carbon\Carbon::parse(
preg_replace('/(\d\d\.\d\d\.)(\d\d) /','${1}20$2 ', '02.01.19 13:49')
);
You can use the method Carbon::createFromFormat() to create a carbon instance from a custom date format string.
Carbon\Carbon::createFromFormat('m.d.y H:i', '02.01.19 13:49');
You can reference the PHP date format string from http://php.net/manual/en/function.date.php
Carbon::createFromFormat("d.m.y H:i", "02.01.19 13:49");
This could solve the issue if the input format is fixed.
I have a date string in this format "2014-12-01 16:00:02,153". How do I parse this to a datetime format for mysql?
I am using laravel and Carbon. My code is Carbon::parse("2014-12-01 16:00:02,153");
but get an error that reads DateTime::__construct(): Failed to parse time string (2014-12-01 16:00:02,153) at position 20 (1): Unexpected character
You could use the createFromFormat method from the DateTime object :
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s,u', "2014-12-01 16:00:02,153");
echo $dateTime->format('Y-m-d H:i:s');
PHP is confused by the time format you are handing over.
new DateTime("2014-12-01 16:00:02,153"); // fails
strtotime("2014-12-01 16:00:02,153"); // fails
This is because of the unexpected part with the comma at the end. Where are you getting this strange timestamp with comma seperated milliseconds from? It is unusual.
As you are asking for datetime which usually does not need any milliseconds you can do the following:
$timeparts = explode(",","2014-12-01 16:00:02,153");
Carbon::parse($timeparts[0]); // should work
$timeparts[1] is the milliseconds then.
If you want to be more precise you can round the milliseconds:
if(intval($timeparts[1]) >= 500) {
// time +1 second
}
... or you treat the milliseconds seperatly if you need em. Add them to the generated timestamp later and check the docs of Carbon.
How to get date format of given string, string contain a valid date
I am try to import data from csv file, and that csv files are exported for backup
but there are so many different type of date format and in some case when i try to convert string to date it throwing me an error
ex: 04/08/2010 10:22 am
some time this type of format throwing me error
Error 500: DateTime::__construct(): Failed to parse time string
You can use just strtotime
echo date('Y-m-d H:i:s', strtotime('04/08/2010 10:22 am')); // output: 2010-04-08 10:22:00
You can use the DateTime::createFromFormat() static method.
Also, you may want to do some reading:
DateTime::__construct() reference
Supported Date and Time Formats
Date/Time extension reference
Also, you need to use the search function (top right on the page). There are a lot of questions that deal with similar problems.
Try this...........
$today = date("d/m/Y g:i a");
<?php
$today = date("d/m/Y g:i a");
echo $today;
?>
You will get output like this.........20/02/2013 7:53 am
For sample one see this example link
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.
I am going round in circles with this one! I'm doing the following:
Retrieving a date from an MSSQL datetime field via SQL/PHP
Sending the date to a new PHP page via the querystring
Trying to use that date in a new SQL query
I'm hitting problems here.
If I use echo:
echo $_REQUEST['rSessionDate'];
output: 15/10/2012
Which is fine, but when I use it in a SQL query I'm not getting the results I expect, so I thought the best thing to do would be to make sure it's being recognised as a date first.
If I use date_format():
echo date_format($_REQUEST['rSessionDate'],'Y-m-d');
output: Warning: date_format() expects parameter 1 to be DateTime, string given in ...
If I use strtotime():
echo strtotime($_REQUEST['rSessionDate']);
output: (nothing)
If I use date():
echo date('Y-m-d H:i',$_REQUEST['rSessionDate']);
output: Notice: A non well formed numeric value encountered in ...
If I use date() with strtotime():
echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate']));
output: 1970-01-01 01:00
I'm sure I'm totally missing something simple.
EDIT: I've tried a few new functions I found:
$rSessionDate = new DateTime($_REQUEST['rSessionDate']);
echo $rSessionDate->format('Y-m-d H:i:s');
output: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (15/10/2012) at position 0 (1): Unexpected character'
and:
$rSessionDate = date_create($_REQUEST['rSessionDate']);
echo date_format($rSessionDate, 'Y-m-d H:i:s');
output: Warning: date_format() expects parameter 1 to be DateTime, boolean given i
EDIT 2:
I have tried using CAST:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) <= CAST('2012-10-15 00:00:00' as DATETIME))
But this fails with error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"
These might help to shed some light on what you're looking for.
http://www.ozzu.com/programming-forum/php-mssql-datetime-field-not-pulling-correctly-t106226.html
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
strtotime() is returning an epoch timestamp in your example above.
or check CAST and CONVERT (refers to MSSQL2000 but may still help you)
http://msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspx
if the date was retrieved from an MSSQL table and you want to use strtotime() in PHP and also don't want to change the date format to yyyy-mm-dd then you can use
CONVERT(VARCHAR(30), DateFromMSSQL, 121) as DateFromMSSQL