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
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've problem with read date value from import value (Excel) to mysql database. i've code as below :
$dataexcel[$i-3]['date_edit'] = date('m/d/Y', strtotime(trim($data['cells'][$i][25])));
i tried use strtotimeto define value from excel, but i've problem to save it into database, if my value in excel with excel date format is 1/1/2018 (it read as 1 Jan 2018, English time format), after i used my code above, it saved become 1970-01-01 it means 1 Jan 1970.
in another example,
i tried another input with date 2/1/2018, it saved into database as 2018-02-02.
from my samples above, in first there missmatch problem with year, then and in the 2nd sample missmatch problem come from date,
so how to declare date to solve my problem, if i want to save date format with simple way?
if there any advice , please, thanks...
From your comment answer, you can modification some code like below :
$date = str_replace("/", ".", $data['cells'][$i][25]);
$dates = date("Y-m-j", strtotime($date));
$dataexcel[$i-3]['date_edit'] = $dates;
$date still pick up from value without used strotime, so date format didn't read correctly...
You can convert the excel date to mysql date like below I did.
//Your input date
$input_date = $sheet->getCellByColumnAndRow(1,$i)->getValue();
$excel_date = $input_date; //here is that excel value 41621 or 41631
//Convert excel date to mysql db date
$unix_date = ($excel_date - 25569) * 86400;
$excel_date = 25569 + ($unix_date / 86400);
$unix_date = ($excel_date - 25569) * 86400;
//echo gmdate("Y-m-d", $unix_date);
//Insert below to sql
$added_date = gmdate("Y-m-d", $unix_date);
A simple function will do this thing
$date = str_replace("/", ".", $data['cells'][$i][25]);// replace the / with .
$date = date("Y-m-d", strtotime($date));
$dataexcel[$i-3]['date_edit'] = $date;
And in the database the default date format is YYYY-MM-DD , so it save correctly to the db
keep it simple. use a defined dateformat when reading the file (the dateformat can be provided as a parameter in case it needs to be adjustable). then, read the date in the following way instead of just using strtotime (taken from php.net's example):
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
relevant example to your code, using the previous $date var:
$dataexcel[$i-3]['date_edit'] = $date->format("y-M-Y");
more info - http://php.net/manual/en/datetime.createfromformat.php
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 form, which includes a date field.
The date will be received in the format dd-mm-yyyy (e.g. 08-11-2015).
I want to convert this to a unix timestamp before inserting into the database.
If possible, I would like to use Codeigniter functions rather than native PHP functions.
Codeigniter has the function:
human_to_unix($input)
The function does not accept a format string and I cannot find the correct format to use ( how will it now that 08-11-2015 is 8th March 2015 rather than 11th August 2015??).
Thanks
The default date_helper in CodeIgniter won't accept dates in the format dd-mm-yyyy. You can use standard PHP (although you specified you would prefer CodeIgniter functions):
$date = DateTime::createFromFormat('d-m-Y', '08-11-2015');
echo $date->format('U');
Alternatively, you can extend the helpers that CodeIgniter provides.
Create a new file application/helpers/MY_date_helper.php:
<?php
function human_to_unix_alternative($datestr) { // change this to a suitable function name
$date = DateTime::createFromFormat('d-m-Y', $datestr);
return $date->format('U');
}
You can then use it in your code, just as you'd use human_to_unix($input), e.g. human_to_unix_alternative($input).
U have to use date format like this to get
human_to_unix -> YY-MM-DD HH:MM <space> meridian
ex:
$this->load->helper('date_helper');
human_to_unix("2015-12-03 03:28 PM");
outputs:
1449136680
$this->load->helper('date');
$input = strtotime('08-11-2015');
$human = unix_to_human($input);
$unix = human_to_unix($human);
Output will be:
2015-11-08 12:00 AM