Laravel PHPExcel convert date into double - laravel-5

I've tried to import an excel file with a column formated DATE by PHPExcel with Laravel 5.4. PHPExcel read the file and output date into double number. The date() function of PHP cannot convert into date for importing into database.
Is there anybody get the same issue?
Update: this is my code
Excel::filter('chunk')
->selectSheetsByIndex(1)
->load(config('excel.import.storage').$fileName)
->chunk(1000, function($rows) {
foreach ($rows as $i=>$row) {
dd($row->toArray());
}
});
The date in excel column is 03/03/2017. The output is 42797.0 So, the date() function cannot work.

Internally, MS Excel stores dates as a float/double, a serialized timetamp (similar to a Unix timestamp, but counting the number of days rather than seconds, and with a different base date). I'm not familiar enough with the Maatwebsite wrapper for laravel to explain why ->formatDates(true) isn't converting that timestamp to a formatted date/time, but PHPExcel does provide native functionality for converting between Excel timestamps and formatted date/times (or indeed to unix timesstamps or DateTime objects in the \PHPExcel_Shared_Date class.

Thanks guys for your comments. I find myself the answer which is easier than I think. You consider the code below to import the Excel files have date columns.
Excel::selectSheetsByIndex(1)
->load(config('excel.import.storage').$fileName, function ($reader){
$rows = $reader->all();
dd($rows[0]->ngay->format('Y-m-d'));
});
The result will be printed as your format string d-m-Y, m-d-Y, ...

Related

only show events who are greater than or equal laravel

Im working in a project where I display different events in my view. The goal for now is to only show events that are upcoming, so it doesn't show events that are older than today.
The users of my page can create an event and set a sertain date for it.
The data is stored into my db like this:
$table->string('datum');
In the view it return it like this:
06.03.2019
My controller to return the data looks like this:
$date = date('d.m.Y');
$evententries = Event::where('datum', '>=', $date)->orderBy('datum', 'asc')->take(3)->get();
but it's somehow not working..
Anyone got any Ideas of how to fix this and what my issue is here?
btw, I'm using laravel 5.7.
Thank you :)
Hmm you are doing it on wrong way from begin...
First: why you are using string to store date value? there is much better options to store date within database...
Second: you are using date format which cannot be compared on that way (using < > =)
when you are comparing strings ie 08.03.2019 will be always smaller than 10.01.2016 because 1 > 0... so if you are comparing dates use:
db format for storing dates
for comparing use format yyyy-mm-dd because on that way even simple string comparison will give you correct result...

Can't remove hours and minutes from datatable

I'm trying to get release date field with y-m-d format.. Actually time format (for y-m-d) seems fine but also it gives h:m:s too, I changed time format at server side and removed h:m:s but datatable still shows them
What I get
2012-04-11 00:00:00
What I want
2012-04-11
Released_at field (Metronic theme - json datatable)
{
field: "released_at",
title: "Release Date",
type: "date",
format: "YYYY/MM/DD"
}
time format (I'm using laravel framework)
protected $dateFormat = "Y-m-d";
How do I fix this ?
Datatable accept column type same as mysql type.
You are trying to convert mysql dateTime column to Date in datatable, which is not possible from datatable.
In your code you have declared
format: "YYYY/MM/DD" // but actual type is dateTime as per mysql.So it will append time next to it.
If you are storing only date in mysql then you can change column type to Date, and your problem gets solved.
And if you want to convert string to date in laravel you can follow this post.
Your database column is of type dateTime which, by definition, includes both date and time information. If you want to remove the time at a database level then use the date type instead
$table->date("released_at")->nullable();
If instead of removing the time from the database, you just want to ignore it for certain parts of your application, you can leverage the fact than in Laravel all dates coming from your models are Carbon\Carbon instances, so you could do
$model->releasedAt->format('y-m-d'); // Returns '18-09-11'

Formatting a date column in powerpointwriter

I am using PowerpointWriter to populate a ppt table. I have currency and date columns that need to be formatted (not with colors/fonts/etc). For example, the date column needs to be the date only, not the time.
How do I do this?
Thanks,
Cheri
PowerPointWriter does not support that kind of formatting at the moment. You should consider formatting your data in your application before binding the data to the PowerPoint file using PowerPointWriter..

Exporting to Excel 2016 in Sortable Date Format

When exporting a query result from SQL Developer to Excel (format xlsx) the dates in my query seem to be exported as text.
I need to filter and sort this data in Excel.
After exporting Excel gives me the option to convert the dates to 20XX, but is there a way to format my dates fields in such a way that I can sort straight away?
My current date format in Preferences is DD-MON-RR
I have a workaround; instead of exporting directly to XLSX, export result as a CSV. Excel is capable of opening such files and - guess what - sorting by date column works fine (I've just tested it). Then, if you want, save file as XSLX.
Yes, I know, this is impracticable, but that's the best I know right now.

Laravel timestamp saving time as unix timestamp

I was wondering what would be the easiest way to change in laravel to save timestamps in database as unix valid timestamp?
Thanks in advance!
Please read about Laravel date mutators:
https://laravel.com/docs/5.1/eloquent-mutators#date-mutators
By default, timestamps are formatted as 'Y-m-d H:i:s'. If you need to
customize the timestamp format, set the $dateFormat property on your
model. This property determines how date attributes are stored in the
database, as well as their format when the model is serialized to an
array or JSON
Also, you can override getDateFormat():
protected function getDateFormat()
{
return 'U';
}
And use this in your migration files:
$table->integer('updated_at');
$table->integer('created_at');
And, if you use soft deletes:
$table->integer('deleted_at');
https://laravel.com/docs/4.2/eloquent#timestamps

Resources