Carbon - standardising datetime - php

I am working on some data migration process, with consists of loads of CSV's
The problem I'm facing now is that the data file I am working with has 2 columns, namely "date" and "time". Now, date itself doesn't have a problem. My "time" column can come in any format, i.e. H:i, or H:i:s. Regardless of the format, I want my output to be d/m/Y H:i:s, and to just append 00 if seconds is not given.
Carbon's createFromFormat requires me to specify what my input format is, which I can't. Currently what I am doing is using Excel to split my CSV files into different files according to the time format, then import them separately, but it's taking too much time.
I can't seem to find a function that allows me to do this either:
if(getSecondsFromTime){
Carbon::createFromFormat($string, 'd/m/Y H:i:s');
}else{
Carbon::createFromFormat($string, 'd/m/Y H:i');
}
Does Carbon/PHP Datetime has a function that I am unaware of that can do this? Or do I have to resolve to regex to do what I need?

If you replace your / with - then Carbon::parse will work:
$t = '05/10/2017 18:00';
$c = Carbon::parse(str_replace('/', '-', $t));

Related

separating a weird formatted timestamp into an understandable date

I'm working with an XML document that is returning variables and for some reason in a xml return the timestamp is formatted like this... 20180606T110000 ... why anyone would format it like that makes no sense to me; however, its what I have to work with. ITs formatted YYYYMMDD , the T is the split between date and time, HHMMSS. ITs set up in a 24 Hour clock that I also need to convert to 12 hr clock with am/pm
I need that formatted like 06/06/2018 11:00:00 AM.
Is there a way to do that via a date format (I know how to use date() but I don't know how to bring in that timestamp the way its formatted) or even separating it out into
$year = xxxx
$month = xx
$day = $xx
$Hour=xx
etc. etc. etc.
if need be.
I've briefly looked at php's date create from format ( date_create_from_format('j-M-Y', '15-Feb-2009') ) but dont fully understand how that works.
I've also thought about a split. I've also looked at chunk_split and wordwrap but its not even amounts of characters so that would be complex to create.
Any ideas?
The format you're working with is "XMLRPC (Compact)" format. This is fully supported by PHP (you can see a list of supported formats here). To get what you want, just use a combination of strtotime() and date().
$timestring = "20180606T110000";
$timestamp = strtotime($timestring);
echo date("m/d/Y h:i:s A", $timestamp);
You can use PHP DateTime to parse a datetime String with any format. Please view the Parameters format in the following link to understand how the "Ymd\THis" part works: http://php.net/manual/en/datetime.createfromformat.php
<?php
$time = "20180606T110000";
$date = DateTime::createFromFormat("Ymd\THis", $time);
// 06/06/2018 11:00:00 AM.
echo $date->format("d/m/Y h:i:s A");

Convert to 12 format time

I have a query for time and display it in a pdf, but it is form to string format i want it to convert it to 12 format time.
This is my codes {nl2br($schedule->cattime)}
laravel is using Carbon as a date/time layer.
to change time/date format in carbon, you need to use createFromFormat method.
Carbon::createFromFormat($format, $time, $tz);
for example, to get 12h time format:
$date = Carbon::createFromFormat('m/d/Y g:ia', '03/10/2017 20:10');
to use carbon from your blade template directly , you will need to use the full namespace:
\Carbon\Carbon::createFromFormat('m/d/Y g:ia', '03/10/2017 20:10');
Update
depending on your comment, you are getting values from database.
it would be easier to fetch formatted time rather than re-format it using TIME_FORMAT function
selectRaw('GROUP_CONCAT(concat(TIME_FORMAT(schedule.start_time, "%r")) separat‌​or "\r\n") as cattime')

Convert and insert two date formats MYSQL

I'm facing an issue with managinging dates, some dates pass others dont. I want to produce an insertable date for mysql. there are two possible types of post dates
yyyy-mm-dd //should go without conversion
m/d/yyyy // should be converted
I'm using this
$date = $_REQUEST['date'];
$date_regex = '/(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d/';
if(preg_match($date_regex, $date)){
$date = DateTime::createFromFormat('m/d/Y',$date)->format('Y-m-d');}
problems
I realised this regex is failing for dates like
2/5/2013
but has been working for
12/12/2013
so I removed it BUT still
DateTime::createFromFormat('m/d/Y',$date)->format('Y-m-d');
is also failing for m/d/yyyy
This date thing has got my head spinning for the last 6 hours.
In this case, there is no need to use DateTime::createFromFormat because m/d/yyyy is one of the recognized date formats (see "American month, day and year"). Just convert it to a DateTime object and let the constructor handle the format and forget the regex:
$date = $_REQUEST['date'];
$datetime = new DateTime($date);
$datex = $datetime->format('Y-m-d');
The reason DateTime::createFromFormat('m/d/Y',$date) fails for dates like 2/5/2013 is because you are forcing it to be specifically 'm/d/Y' and that date does not fit that pattern. You can see a list of all date formats here. Specifically, m expects there to be a leading zero (like 02), so when you give it one without that, it won't recognize it. Same goes for d. In this case you would have to use n and j respectively. But, like I said, let the constructor do the hard work for you.

import an excel file with PHP Excel Reader

I'm trying to use this class to import a large amount of data. Most of the data is being read correctly, however I have two date columns which are giving me problems.
The dates are in the format DD/MM/YYYY and the values returned are one day ahead of those in the spreadsheet. For example, 04/03/2011 00:00 becomes 04/03/2011 02:00
I have tried accessing the data like this:
$data->sheets[$sheet]['cells'][$row][$col];
I have also tried using the raw data:
$data->sheets[$sheet]['cellsInfo'][$row][$col]['raw']
Which returns the date as a unix timestamp but still it is one day ahead of what it should be.
Is there any way I can force the class to return the value of the column as a simple string?
The solution is simple - why don't you just deduct a day from the timestamp, or from the date you fetch?
$wrongDateTimestamp = "1304398800";
$rightDateTimestamp = strtotime("-1 day", $wrongDateTimeStamp); // Or alternatively - $wrongDateTimeStam - 86400
$rightDate = date("d/m/Y", $rightDateTimestamp);
Hope this helps.Shai.

Function to parse psql timestamp

I display the date or the time on my website a lot and I'm thinking about writing a function to parse a PostgreSQL timestamp.
The timestamp is in the format: Y-m-d H:i:s.u. E.g. 2011-04-08 23:00:56.544.
I'm thinking about something like this:
function parse_timestamp($timestamp, $format = 'd-m-Y')
{
// parse the timestamp
return $formatted_timestamp;
}
However I am wondering whether this can also be achieved without writing a parser for it myself (with the use of some PHP function).
function parse_timestamp($timestamp, $format = 'd-m-Y')
{
return date($format, strtotime($timestamp));
}
Don't forget to set timezone before, e.g.
date_default_timezone_set('UTC');
Or in your case, I guess 'Europe/Amsterdam'.
You can always get PHP timestamp of this format Y-m-d H:i:s.u using strtotime(). Then, using date() you can export time in your own format. Both functions depend of time zone set.
strtotime is perfectly capable of parsing that time string, then just reformat it with date:
echo date('d-m-Y', strtotime('2011-04-08 23:00:56.544')); // 08-04-2011
For those using DateTime class:
DateTime::createFromFormat('Y-m-d H:i:s.u', $yourTime);
If the database isn't giving you what you want, change it. PostgreSQL can also format dates and times.
select to_char(timestamp '2011-03-04 07:04:00', 'DD-MM-YYYY');
04-03-2011
But that's risky in international contexts, like the web. Different locales expect different ordering of elements. (Does "04-03" mean 03-April or 04-March?) This expression is easier to understand, and it uses locale-specific abbreviations for the months.
select to_char(timestamp '2011-03-04 07:04:00', 'DD-Mon-YYYY');
04-Mar-2011
Take a look at the strptime function - it can parse many time formats.

Categories