convert string date to date format in laravel [duplicate] - php

I'm trying to parse a date formated like :
2017-09-20T10:59:10.0000000 01:00
I'm using Carbon, so i tried :
Carbon::createFromFormat('Y-m-dTH:i:s.u vP', $date)
Which output :
The timezone could not be found in the database\n
Unexpected data found.\n
Data missing
I guess the last timezone argument maybe wrong but i couldn't find how to parse that date format :/
Thanks for help !

You'll need to add a sign to the timezone, for example:
+01:00
Then this will work for you:
Carbon::createFromFormat('Y-m-d\TH:i:s.0000000 P', $date)
If your string can have -01:00 but instead of +01:00 you're getting 01:00, do this first:
$timezone = str_after($date, ' ');
if ($timezone[0] !== '-') {
$date = str_before($date, ' ') . ' +' . $timezone;
}

Related

Check if a date is equal to other date using Laravel 5.3

I am trying to check if one date is equal than the other date, but I can't get the match because the date format coming from the form turns into a different order once it gets through the "parse" code.
I need to format this date to find the match, here is a sample code to show how I am trying:
...
// $ago will give me this date: 2016-12-09 00:00:00
$ago = Carbon\Carbon::today()->addDays(2); // Todays date + 2 days
//$request->datex has the date coming from a form with this format, '12-06-2016'.
// Once a parse $request->datex here, the date gets out of order:
$my_date = Carbon\Carbon::parse($request->datex);
// it shows the date like this, 2016-09-12 00:00:00 , I need it to be on this format: 2016-12-09 00:00:00
// then I could do this:
if ( $ago$ == $my_date ) {
dd($my_date.' is equal to: '.$ago );
}else{
dd(' Not equal!');
}
...
Thanks for looking!
Change this line
$my_date = Carbon\Carbon::parse($request->datex);
with this:
$my_date = Carbon::createFromFormat('m-d-Y', $request->datex)
I've assumed that your format '12-06-2016' means DAY-MONTH-YEAR
UPDATE
Tested my solution on my machine and it works, date is recognized properly:
When
$request->datex = '12-06-2016'
then
$my_date = \Carbon\Carbon::createFromFormat('m-d-Y', $datex);
gives me date like that: public 'date' => string '2016-12-06 18:52:09.000000' (length=26)
Date has been parsed properly. The thing that I've assumed just now. These dates won't be same cause of hours, minutes, seconds and milliseconds. To fix that just we have to compare dates that way:
if ( $ago->format('Y-m-d') == $my_date->format('Y-m-d') )
//do something awesome with our equal dates
PHP expects DD-MM-YYYY or MM/DD/YYYY formats.
If you always have a MM-DD-YYYY format, you could do this before parsing:
$request->datex = str_replace('-', '/', $request->datex);

A non well formed numeric value encountered from XML feed?

I am getting feeds from XML feeder and when i try to convert it's date values, i am getting notice:
A non well formed numeric value encountered on line 61
Feeds returns date in this format:
24.06.2015
and time in this format:
12:00
I tried to convert it like this:
$dbdate = date('Y-m-d',strtotime($match->attributes()->date));
$dbtime = date('h:i', strtotime($match->attributes()->time));
$datetime = date('Y-m-d h:i',$dbdate . ' ' . $dbtime);
In database it is converted as i want, but i get notice before that.
What i am doing wrong?
UPDATE
SOLVED, i forget to add strtotime in $datetime :
$datetime = date('Y-m-d H:i',strtotime($dbdate . $dbtime));

stringtotime beginning and end of the month not working proppely

I have the code:
$month = DateTime::createFromFormat('m/Y', $date);
if ($month) {
$month = $month -> format('01/m/Y');
}
echo "From ".$startMonth." to ".$endMonth = date("t/m/Y", strtotime($month));
when $date is a string like "05/2015".
This returns:
From 01/05/2015 to 31/01/2015
But for some reason the month is coming up as 01 when it should be 05?
Why is it doing this? It should be
From 01/05/2015 to 31/05/2015
Besides the unnecessary chopping and changing between DateTime objects and unix timestamps (when you could do the whole thing using DateTime objects.... you're passing a formatted date of '01/m/Y' to strtotime()... the / indicates to the strtotime() function that the date is in US date format (mm/dd/yy): and you should use '01-m-Y' (with a -) for dd-mm-YYYY if you want to tell strtotime() that it's a European format date.
See the "localized notations" table on the PHP Docs page for an explanation of formats accepted by strtotime()
However, doing the whole thing using DateTime objects:
$date = '4/2015';
$month = DateTime::createFromFormat('m/Y', $date);
echo "From " . $month->format('01/m/Y') . " to ". $month->format('t/m/Y');

Converting time stamp

I have in issue with this code, I'm reusing it from a different script, it is reading from an xml file and converting the date/time from a node. The date in the node is as follows which is the only difference to the original script:
<od>10:15:41 01/03/13</od>
I thought I had this modified correctly but it isn't working:
$_date=$record->getElementsByTagName("od");
$_date=((!empty($_date))?$_date->item(0)->nodeValue:"");
if(strpos($_date,".")!==false)
{
$_date=substr($_date,0,strpos($_date,"."));
}
$_date=date("H:i:s m/d/Y",strtotime($_date));
$_date.=(trim($_date)!="")?"Z":"";
xmlrpc_set_type($_date, 'datetime');
Any help is much appreciated.
The date/time 10:15:41 01/03/13 is an invalid format.
Use DateTime::createFromFormat instead.
strftime will work fine with a Y-m-d H:i:s format as it's unambiguous.
On the other hand, it gets confused with H:i:s m/d/y, as it can be interpreted as H:i:s d/m/Y. Think about the date 02/03/2013 - m/d/y would suggest that it's the 3rd of Feb, whereas d/m/Y would suggest that it's 2nd of March.
In other words, to ensure we get the right date every time, we have to be more specific. date_create_from_format('H:i:s m/d/y', $_date) will give you a DateTime object corresponding to the correct date, if the date given is indeed in the 'H:i:s m/d/y' format.
// Retrieve the date string
$_date=$record->getElementsByTagName("od");
$_date=((!empty($_date))?$_date->item(0)->nodeValue:"");
// Standardize it
$_date = get_date( $_date );
$_date .= (trim($_date) != "") ? "Z" : "";
xmlrpc_set_type($_date, 'datetime');
function get_date( $rawDate ) {
// Clean date string
if(strpos($rawDate,".")!==false) {
$rawDate=substr($rawDate,0,strpos($rawDate,"."));
}
// Attempt converting from m/d/y AND m/d/Y formats
$date = date_create_from_format('H:i:s m/d/y', $rawDate);
if( false === $date ) $date = date_create_from_format('H:i:s m/d/Y', $rawDate);
if( !empty($date) ) {
return $date->format('H:i:s m/d/Y'); // Convert the date to a string again
}
// If neither works, try using strtotime instead
$date = #strtotime($rawDate);
$date = !empty($date) ? date('H:i:s m/d/y', $date) : false;
return $date;
}
Hope that helps!

Reformat a date in PHP

I have a date/time string like this: 180510_112440 in this format ddmmyy_hhmmss
I need a snippet for having a string formatted like this way: 2010-05-18 11:24:40
Thanks for help.
another possible answer is the common use of strptime to parse your date and the mktime function:
<?php
$orig_date = "180510_112440";
// Parse our date in order to retrieve in an array date's day, month, etc.
$parsed_date = strptime($orig_date, "%d%m%y_%H%M%S");
// Make a unix timestamp of this parsed date:
$nice_date = mktime($parsed_date['tm_hour'],
$parsed_date['tm_min'],
$parsed_date['tm_sec'],
$parsed_date['tm_mon'] + 1,
$parsed_date['tm_mday'],
$parsed_date['tm_year'] + 1900);
// Verify the conversion:
echo $orig_date . "\n";
echo date('d/m/y H:i:s', $nice_date);
$inDate = '180510_112440';
$date = strtotime('20'.substr($inDate,4,2).'-'.
substr($inDate,2,2).'-'.
substr($inDate,0,2).' '.
substr($inDate,7,2).':'.
substr($inDate,9,2).':'.
substr($inDate,11,2));
echo date('d-M-Y H:i:s',$date);
Assumes date will always be in exactly the same format, and always 21st century
list($d,$m,$y,$h,$i,$s)=sscanf("180510_112440","%2c%2c%2c_%2c%2c%2c");
echo "20$y-$m-$d $h:$i:$s";

Categories