I have an Excel file, where I have a date column. The date looks like "17.07.2020".
I'm using Laravel and Maatwebsite Excel import. When I'm saving the imported data to MySQL, the date in MySQL is always "1970-01-01".
Here is the code, which I have now:
return new Product([
.......
'discountBeginning' => date('Y-m-d', strtotime($row['discount_beginning'])
]);
I would like to format the date from Excel to "2020-07-17".
Try this
$date = new DateTime($row['discount_beginning']);
echo $date->format('d.m.Y');
You are using Laravel, so you already have access to the awesome Carbon library. For how data is formatted in your excel file (day.month.year), you can use it like:
return new Product([
.......
'discountBeginning' => \Carbon\Carbon::createFromFormat('d.m.Y', $row['discount_beginning']),
]);
Once you have the carbon instance, you can format it however you would like according to php's date syntax like:
$date = \Carbon\Carbon::createFromFormat('d.m.Y', $row['discount_beginning']);
$date->format('Y-m-d');
$date = "17.07.2020";
$d2 = \Carbon\Carbon::createFromFormat('d.m.Y', $date)->format('Y-m-d');
dd($d2); // returns: "2020-07-17"
you can also try to append ->toDateString(); if your returned date is not a string. If it is already a string, this will fail.
Related
I set in my excell file for format date like 09/01/19 (dd/mm/yy)
my function to get format date in excell like this
$form = $sheet->getCellByColumnAndRow($j, $i)->getStyle()->getNumberFormat()->getFormatCode();
but when im try to call with format excell, the result is 0909/0101/1919
$vax = '2019-01-09 03:09:00'; $createDate = new DateTime($vax);
$vax = $createDate->format($form); }
In php documentation you can see [here][1]
[1]: http://php.net/manual/en/datetime.format.php, it requires
$createDate->format($form); // in here you need to give ur format like this 'Y-m-d H:i:s'
but what you have done is giving it the format from ur excel like this 09/01/19
this does not recognize by the php method. so change the input for that function to 'Y-m-d H:i:s' format
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
so change your mehtod like this
$createDate = new DateTime($vax);
$vax = $createDate->format('Y-m-d H:i:s');
MS Excel has its own date formatting masks that Microsoft created before PHP even existed - https://support.office.com/en-us/article/Format-a-date-the-way-you-want-8E10019E-D5D8-47A1-BA95-DB95123D273E - and which isn't the same as that used by PHP. If you use the getFormatCode() method in PHPExcel/PHPSpreadsheet, it will return the MS Excel format mask
I have a large excel sheet which I am inserting data by bulk upload.
My problem is date:
date format is like 09/11/2017. I need to insert this data into table.
Any one can suggest me how to do this by conveting in mysql date format?
Use strtotime() on your first date then date('Y-m-d') to convert it back:
$dateFromExcel = strtotime('09/11/2017');
$newDateToInsert = date('Y-m-d',$dateFromExcel);
echo $newDateToInsert;
// 2017-11-09
if you are using PHP 5.2 or higher
use
$ymd = DateTime::createFromFormat('d/m/Y', '09/11/2017')->format('Y-m-d');
you need to get row first and then use like this.
This is a function to convert date from DD-MM-YYYY to YYYY-MM-DD :
function con2mysql($date) {
$date = explode("-",$date);
if ($date[0]<=9) { $date[0]="0".$date[0]; }
if ($date[1]<=9) { $date[1]="0".$date[1]; }
$date = array($date[2], $date[1], $date[0]);
return $n_date=implode("-", $date);
}
If you are using phpExcel library to uplod exccel file than you can use following line.
It works perfectly for me.
PHPExcel_Style_NumberFormat::toFormattedString($exceldate, 'YYYY-MM-DD');
I am using DateTime function of php. I get a date from a calendar in format d-m-Y and pass it via ajax to my function. I am getting the date right till this step.
When I try to store the date in unix format using:
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y', $data['date']);
$final_date=$ai_ff_date->format('U');
The date stored is wrong. Suppose the date I passed via ajax is 26-12-2016 then in database 27-12-2016 is stored. Why its counting one more day then the input.
use this code :
$date = date('Y-m-d H:i:s', strtotime('-1 day', $stop_date));
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y',$date);
$final_date=$ai_ff_date->format('U');
and please check the variable (code not tested)
You might want to convert the Date-Format to "Y-m-d" First and then call-in the DateTime() Constructor. However, since what you are trying to do is just get the TimeStamp you might also do that directly without using DateTime. The Snippet below shows what is meant here:
<?php
$data = ['date'=>"13-12-2016"]; //<== JUST AN EXAMPLE FOR TESTING!!!
// SIMPLY CONVERT THE DATE TO Y-m-d FIRST.
$dateYMD = date("Y-m-d", strtotime($data['date']));
// THEN USE DateTime CONSTRUCTOR TO CREATE A NEW DateTime INSTANCE
// AND THEN RUN THE FORMAT YOU WISH::
$final_date = (new DateTime($dateYMD))->format('U');
var_dump($final_date); //<== YIELDS: string '1481583600' (length=10)
var_dump(date("Y-m-d", $final_date)); //<== YIELDS: string '2016-12-13' (length=10)
I have a problem trying to upload an excel to my database through PHP.
The format I have in the excel is dd/mm/yyyy and my sql needs yyyy/mm/dd
$_DATOS_EXCEL[$i]['ship_day']= $objPHPExcel->getActiveSheet()- >getCell($headings['Ship Date'].$i)->getFormattedValue();
I tried
getFormattedValue('YYYY/MM/DD');
but doesnt work
I really appreciate if you can help me out with this.
If your cell value really is an Excel serialized date/time value, and you know which cells contain these dates, then you can convert it directly to a PHP DateTime object
$dto = PHPExcel_Shared_Date::ExcelToPHPObject(
$objPHPExcel->getActiveSheet()
->getCell($headings['Ship Date'].$i)
->getValue()
);
$_DATOS_EXCEL[$i]['ship_day'] = $dto->format('Y/m/d');
or to a Unix Timestamp
$unixTimestamp = PHPExcel_Shared_Date::ExcelToPHP(
$objPHPExcel->getActiveSheet()
->getCell($headings['Ship Date'].$i)
->getValue()
);
$_DATOS_EXCEL[$i]['ship_day'] = date('Y/m/d', $unixTimestamp);
Add three line after
"$_DATOS_EXCEL[$i]['ship_day']= $objPHPExcel->getActiveSheet()- >getCell($headings['Ship Date'].$i)->getFormattedValue();"
this line......
$rearrange=$_DATOS_EXCEL[$i]['ship_day'];
$explode = explode("/",$rearrange);
$_DATOS_EXCEL[$i]['ship_day']= $explode[2]."/".$explode[1]."/".$explode[0];
I am trying to format a datetime variable with the following code:
$passed_time = $stu_quiz->c_date_time;
$passed_time_string = date_format($passed_time, 'M-d-Y');
For some reason, if I print $passed_time_string, the output is blank, but if I print out $passed_time, I get the date (in the format 2011-06-15 21:43:09).
Why is the date_format method not working?
The date_format function expects a "DateTime" object that is created using date_create.
Example:
$passed_time = date_create($stu_quiz->c_date_time);
$passed_time_string = date_format($passed_time, 'M-d-Y');
You are looking for just date()
http://php.net/manual/en/function.date.php
If you don't have PHP 5.3 and cannot use the DateTime class, try this:
$passed_time_string = date("M-d-Y", strtotime($passed_time));
First it converts your original MySQL time to a unix timestamp, then formats it as M-d-Y