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');
Related
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.
I want to upload data from csv file and my database only accept yyy-mm-dd date format but sometimes users will enter data as dd/mm/yyy date format so I am tried to do following method using Keboola\Csv
if($header[$c]=='date' && $row[$c]!='')
{
$date_text=['--na','--N/'];
$sexe_text=str_replace($date_text,'N/A',$row[$c]);
if(in_array(array('--na','--N/'),$row))
{
$newRow[$c]=$sexe_text;
}
else
{
$date_array=explode('/', trim($row[$c]));
$date=trim($date_array[0]);
$month=trim($date_array[1]);
$year=trim($date_array[2]);
$date_time=$year.'-'.$month.'-'.$date;
$newRow[$c]=$date_time;
}
}
But it will revert again if that cell format in dd/mm/yyy so Now I want to change that cell format into date YYY-MM-DD how can I do it using PHP
You can direct convert it using date() function.
$exceldate = trim($row[$c]);
$date = date('Y-m-d',strtotime($exceldate));
echo $date;
// 2019-08-09
PHP has already predefined date() to convert dates in any format
Simply replace your code with the code given below
if($header[$c]=='date' && $row[$c]!='')
{
$row[$c] = date('Y-m-d', strtotime($row[$c]));
}
else
{
# if $row[$c] is null, do whatever you want.
}
The PHP date() accepts date in any format, wheather it is separated by forward slashes or hyphens.
To learn more you can see this documentaion
for date() : https://www.php.net/manual/en/function.date.php
and for strtotime() : https://www.php.net/manual/en/function.strtotime.php
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];
<?php
// Convert MMDDYY to YYYY-MM-DD date format to be INSERTed
// into MySQL TABLE.
// If it can't convert the data because of an error, it returns 0000-00-00.
date_default_timezone_set('America/New_York');
function date_convert($date_input) {
try {
$date = new DateTime($date_input);
$date_processed = $date->format('Y-m-d');
} catch(Exception $e) {
echo "Wrong date format $date_input" . "\n";
$date_processed = "0000-00-00";
}
return $date_processed;
}
// MMDDYY (January 1, 2000)
$TRANSACTION_DATE_MMDDYY_raw = "010100";
echo date_convert($TRANSACTION_DATE_MMDDYY_raw) . "\n";
?>
Output:
2013-06-28
I'm expecting it to return 2000-01-01, but it's returning today's date. What am I missing? How can I fix this? Thanks!
You should be using the createFromFormat method. See: http://us2.php.net/manual/en/datetime.createfromformat.php
PHP has no way to know your date is formated like MMDDYY. There are two ways to solve this:
use one of the 'valid formats'. You can eather change your input value, or parse it to a valid format with some string functions or a regex.
use the static createFromFormat method that comes with the DateTime class.
As I presume changing the input is not an option, and parsing the input seems a bit far fetched, using the second method seems the most appropriate here, and would result in changing this:
$date = new DateTime($date_input);
into this:
$date = DateTime::createFromFormat('mdy', $date_input);
The format you specified can't be converted/isnt recognized to be a proper format. Try '2000-01-01'
I have get data from oracle database and date value kept on 27-MAY-09. I need to insert this value to mysql database via PHP. I need to convert date format as 2009-05-27.
Any one know about it please let me know correct php statement for do this.
use date() function
$date = '27-MAY-09';
$newData = date('Y-m-d', strtotime($date));
php fiddle
$date = DateTime::createFromFormat('j-M-y', $inputDate);
$newDate = $date->format('Y-m-d');
PHP 5.3 not earlier.
try this
$date1 = "27-MAY-09";
$data2 = date("Y-m-d",strtotime($date1));