1

I have a problem to set my date format using PHPExcel on Codeigniter

I already have controller and model and view that you can access

Model: http://sistempintar.com/file/LingkunganModel.txt

Controller: http://sistempintar.com/file/Lingkungan.txt

View: http://sistempintar.com/file/form.txt

I make a preview before import to a database, but an error that says

"A PHP Error was encountered Severity: Warning

Message: date_create(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.

Filename: Shared/Date.php

Line Number: 167

Backtrace:

File: /home/u4502442/public_html/sistempintar/demo/proyek/application/third_party/PHPExcel/PHPExcel/Shared/Date.php Line: 167 Function: date_create

File: /home/u4502442/public_html/sistempintar/demo/proyek/application/third_party/PHPExcel/PHPExcel/Style/NumberFormat.php Line: 454 Function: ExcelToPHPObject

File: /home/u4502442/public_html/sistempintar/demo/proyek/application/third_party/PHPExcel/PHPExcel/Style/NumberFormat.php Line: 607 Function: _formatAsDate

File: /home/u4502442/public_html/sistempintar/demo/proyek/application/third_party/PHPExcel/PHPExcel/Worksheet.php Line: 2493 Function: toFormattedString

File: /home/u4502442/public_html/sistempintar/demo/proyek/application/third_party/PHPExcel/PHPExcel/Worksheet.php Line: 2556 Function: rangeToArray

File: /home/u4502442/public_html/sistempintar/demo/proyek/application/controllers/Lingkungan.php Line: 51 Function: toArray

File: /home/u4502442/public_html/sistempintar/demo/proyek/index.php Line: 315 Function: require_once"

and this is my date.php: http://sistempintar.com/file/date.txt

this is the picture that shows error :

http://sistempintar.com/file/format_database.png

I don't' know what is wrong, please help

Danish Ali
  • 2,354
  • 3
  • 15
  • 26
Ricky J
  • 11
  • 2
  • [In your php.ini config](https://stackoverflow.com/a/32227023/61795), or [before you start calling](http://php.net/manual/en/function.date-default-timezone-set.php) your date functions `set date.timezone to select your timezone` as per the error message. – Scuzzy Jan 15 '19 at 11:38
  • on my date.php you mean ? – Ricky J Jan 15 '19 at 11:47
  • even i set date.timezone to my timezone, the error still same – Ricky J Jan 15 '19 at 11:56
  • okay, what error are you trying to fix? the date error or the function error? – FreedomPride Jan 16 '19 at 03:59
  • When process to input from excel (after preview) to database, there are error that the dates on database will 0000-00-00, but when i try it 121212, on database it will be 2012/12/12 – Ricky J Jan 16 '19 at 18:28

3 Answers3

0

You need to update your PHP timezone settings. In your php.ini file, look for this line: date.timezone = and set it to your timezone. You can find a list of supported Timezones here: here

0

The solution for this problem is that, we have to convert the format of date from date to number using option Format Cells so our date will display some kind of numeric data on the cell.

This is because excel use a simple number formatting to read dates like 1/1/1900 = 1.00 and 16/1/2012 = 40924.00. So whenever we have to upload dates using excel we have to convert it into number and then upload it and just before the insert query of database we use a function which is listed below to convert the number format into date format which support by database.

function excel_number_to_date($num)

{

$num=$num-25570; //this is because php date function work only for date after 1/1/1970

return addday(‘1970/01/02’,$num);

}

The static no. 25570 denoted a date of 1/1/1970. Because while we upload data in MySQL than it will not read date which is older than 1/1/1970. Thants why I have to subtract this particular no. IT totally depends on type of database and addday function is listed below.

function addday($dat,$days) //$date will be in YYYY/mm/dd format

{

$dat=str_replace(“/”,”-“,$dat);

$dat=date(“Y-m-d”,strtotime($dat));

return date(“Y-m-d”,strtotime($days.’ days’,strtotime($dat)));

}
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
Irfan Shaikh
  • 105
  • 1
  • 3
  • 12
0
date_default_timezone_set('Europe/Amsterdam');

You can try this line of code to set your timezone in CodeIgniter.

You can get a list of time zones to use from here

pr1nc3
  • 8,108
  • 3
  • 23
  • 36