import an excel file with PHP Excel Reader - php

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.

Related

DATE and TIME with PDO PHP MYSQL

I'm unable to get the TIME for datetime datatype. I do get date but not able to get time and insert into database using PDO PHP. What needs to be done in the following code to get the time along with date.
I tried with H:i:s but it inserts data as 1970-01-01 00:00:00
Date format for input is 08/31/2020 - 05:04 pm
I am looking for date and time(hours and minutes).
$dp = date_create_from_format('m/d/Y',$_POST['dp']);
//CHECKS VALUE
$stmt->bindValue(':dp', $dp->format('Y-m-d'), PDO::PARAM_STR);
//GETTING POST VALUES
$dp=$_POST['dp'];
$dp = date('Y-m-d', strtotime($dp));
//BINDING PARAMETERS
$query->bindParam(':dp', date('Y-m-d', strtotime($dp)));
NOTE: All the above code works fine with getting and inserting date but does not work with date and time both.
There are two problems:
Your input string is in an unusual format. Therefore you need to use the DateTime::createFromFormat method to parse it (strtotime() can't do the job) using the correct format string.
Your output format is missing the time component - you need to add hours (in 24hr format), minutes and seconds to the string.
Here's a working example:
$dp = DateTime::createFromFormat("m/d/Y - H:i a", $_POST["dp"]);
$dpstr = $dp->format('Y-m-d H:i:s');
$query->bindParam(':dp', $dpstr);
Assuming $_POST["dp"] contains "08/31/2020 - 05:04 pm" then $dpstr will be 2020-08-31 17:04:00.
Demo: http://sandbox.onlinephpfunctions.com/code/b4cc0988c4eee60061502d86f38eccfc97aa9a49

PHPExcel putting string date into .xls

guys. I have dates at DB as strings "d/m/Y".
When I put this dates into Excel, it shows me normal dates like 03/10/2000, but when I click on the cell, it show me the value '03/10/2000. So, the value is string, and not date as required for me.
So, I transformed the date from DB to a PHP date object and get sure with var_dump that date is Date object with that code :
$date = date_create_from_format('d/m/Y', $array[$i-20]['Date']);
$date->format('m/d/Y');
but anyway, the values in generated .xls are with ' before value.
Why it appears and how can I solve it ?
MS Excel stores dates as a serialized timestamp, the number of days since 1st January 1900 (or 1st January 1904 if using the Mac calendar). To store a value as a date (and not simply as a string), you need to convert your string date to a serialized timestamp, and then set the format mask for the cell.
$date = date_create_from_format('d/m/Y', $array[$i-20]['Date']);
$excelDate = PHPExcel_Shared_Date::PHPToExcel($date);
Set $excelDate as the cell value
$objPHPExcel->getActiveSheet()->setCellValue('C11', $excelDate );
and then apply the format mask to that cell
$objPHPExcel->getActiveSheet()
->getStyle('C11')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
The 02types.php example script in the `/Examples` folder demonstrates this
EDIT
You can (and it's more efficient to) apply a style to a range of cells: just specify the range in the getStyle() call.
$objPHPExcel->getActiveSheet()
->getStyle('C11:C200')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
To get details of available format: PHPExcel has a number of pre-defined formats built-in, and those can be found as constants in Classes/PHPExcel/Style/NumberFormat.php. However, you're not limited to that set of formats; and (in the same way that MS Excel lets you define "custom" formats) you can simply pass a string defining the format (e.g. "d-mmm-yyyy") using pretty much any of the rules that can be used for MS Excel number format masks as the format

Carbon - standardising datetime

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));

PHP Date Issue with Excel Sheet

I am trying to convert an old office excel sheet document data into PHP web based app for searching and other data analysis. Stuck in how to fields called start_date & end_date. This tells me that if project is active or not. Data saved in below format. It compares both dates and return the project status by comparing with current date.
Start_Date End Date Status
1-Jan-16 31-Dec-16 Active or not
I tried with PHP date("Y-m-d") function but month is an issue.
You can use the strtotime() function on your excel derived dates and then convert it to a usable DateTime value using date() like so:
$time_before = strtotime('1-Jan-16');
$date_before = date('Y-m-d', $time_before); // 2016-01-01
$time_after = strtotime('31-Dec-16');
$date_after = date('Y-m-d', $time_after); // 2016-12-31
Then compare the two dates against each other.

How to insert time with am/pm format into the database

So im trying to insert a time using an input text field into a database table with data type TIME.
The format of time that I want to insert should be like this:
H:MM pm// example: 6:30 pm
The problem is the am/pm doesnt insert in my database table.
Only the hour and minute.
Please give me idea how to solve this.
Better with sample codes. Thanks.
Data Type TIME is for storing time data type - that means no AM/PM. Store the data in Your database in 24 hour format and format it to 12 hour format with am/pm in PHP or MySQL using one of these:
PHP:
$date = new DateTime($mysql_column['time']);
$date->format('h:i:s a');
or:
$date = date('h:i:s a', strtotime($mysql_column['time']));
or MySQL:
SELECT DATE_FORMAT('%h:%i:%s %p', time) FROM table;
Store the TIME as a standard format (18:30:00), and the format it however you want when you display it (Using DateTime objects or the date functions).
MySQL doesn't support extra formats when storing time data.
I think you want to add the jquery time picker value in your database with actual format in the database.
Here I have written some function
function update_time($time){
$ap = $time[5].$time[6];
$ttt = explode(":", $time);
$th = $ttt['0'];
$tm = $ttt['1'];
if($ap=='pm' || $ap=='PM'){
$th+=12;
if($th==24){
$th = 12;
}
}
if($ap=='am' || $ap=='AM'){
if($th==12){
$th = '00';
}
}
$newtime = $th.":".$tm[0].$tm[1];
return $newtime;
}
$time = update_time($_POST['time']); //here I am calling the function now you can insert the value in db
you just have to call the function and insert the returned value in database.
And while printing that you can do something like that echo date("h:i A",strtotime($time));
Change the type of the field to a varchar. TIME cannot store it like that. However, keep in mind that storing it like you want to will make it more difficult to provide localized results if that is something you will eventually need. That is, timezone support becomes difficult if you are not storing the timestamp itself, but rather a user-friendly representation.
EDIT: Or, DATETIME works as well, as was pointed out in the comments above.
You can use the DateTime Object in PHP which has functions to create a time object from any format and also has a function to output a time in any format like so
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
http://php.net/manual/en/datetime.createfromformat.php
You would be best changing the field type to 'VARCHAR (32)', and then writing the time with PHP.
Example: date('m/d/y g:i:sa');
Why do you want to store the am or pm anyhow? If you store the date/time as a unix epoch timestamp, you can format the date however you want in the program - not the database.
Example: time(); - Store this in an INT(8) field.
date('m/d/y g:i:sa, $time()); - Output from DB like this.
try .ToShortTimeString() after your date variable.

Categories