Export date to Excel

Export date to Excel

by Todd Almarez -
Number of replies: 23

I'm using Moodle 1.9 and Excel 2010.  When I export a date within a database, the date is displayed as a UNIX timestamp.  I have to create a formula within Excel to convert this to a number Excel can interpret as a date and then format the cells to read as a date.. 

It is not practical for all my students to have to apply a formula each time they export in order to see the date.  I've tried modifying the excellib.class.php file to convert the UNIX timestamp into an Excel readable number with no luck. Am I missing something?

Can I modify the database to properly export the date in an Excel-readable number or am I stuck with the UNIX timestamp?

I've attached the excellib.class.php file as a reference if anyone would like to give it a shot.

Thanks in advance!

Average of ratings: -
In reply to Todd Almarez

Re: Export date to Excel

by Itamar Tzadok -

This is not excel issue so you don't really need to touch the excellib. Instead you can simply override in the data_field_date class, the data_field_base export_text_value function to someting like:

    function export_text_value($record) {
        if ($this->text_export_supported()) {
            return userdate($record->content, get_string('strftimedate'), 0);
        }
    }

Of course, exported this way, the date field will not be importable but this may not be an issue for you.

hth smile

In reply to Itamar Tzadok

Re: Export date to Excel

by Todd Almarez -
I'm fairly new to php and Moodle so forgive my inexperience. I'm having trouble locating the file that needs to be overridden. Is the file located in mod - data - field - date folder? In that folder there is a field.class.php file but it does not contain a function export_text_value line.
In reply to Todd Almarez

Re: Export date to Excel

by Itamar Tzadok -

That's the file and you can simply copy the function code I posted above and paste it there inside the class definition (that is, before the class closing '}'). Currently this function is defined for the date field only in the parent class which exports the field value as stored in the database. Adding the function, as posted above, to the date field class would override the parent's and the date field should be exported as it appears in the entry view. smile

In reply to Itamar Tzadok

Re: Export date to Excel

by Todd Almarez -

Got it!  Works like a charm!

Many thanks!

big grin

In reply to Todd Almarez

Re: Export date to Excel

by John Samuel -

Hi there,

I have the Datetime profile field and eachtime i export the grades from gradebook to excel, this field shows bunch of numbers that don't make sence . how did you guys get it to work?

Thank you

In reply to John Samuel

Re: Export date to Excel

by Itamar Tzadok -

Post a screenshot so that we see what these numbers are. smile

In reply to Itamar Tzadok

Re: Export date to Excel

by John Samuel -

here it is

Attachment 2012-02-13_1704.png
In reply to John Samuel

Re: Export date to Excel

by Itamar Tzadok -

The relevant code is in grade/export/xls/grade_export_xls.php (2.2).

But since you know how to export the start date as an extra bit of information you should already have modified the code there (around line 51) and should have something like:

        $myxls->write_string(0,6,get_string("startdate"));
        $pos=7;

So you just need to convert the timestamp right before writing it into the xls worksheet (around line 76): 

            $myxls->write_string($i,6,userdate($user->startdate, get_string('strftimedate'), 0));
            $j=7;

hth smile

In reply to Itamar Tzadok

Re: Export date to Excel

by Daniel Lombardo -

Hi Itamar

I'm sorry to disturb you. I know this is an unrelated topic, but it may have to do with the grade_export_xls.php file. I am looking for someone who can tell me how to export custom user profile field data with the grade export. Please see this post for where the issue is located if you feel you can help us.

Thanks in advance

In reply to Daniel Lombardo

Re: Export date to Excel

by Itamar Tzadok -

No disturbance at all and yes I can help and I've just messaged you to continue by email. smile

In reply to Itamar Tzadok

Re: Export date to Excel

by Hilton Sato -

Hi Itamar

I've found grade_export_xls.php. However, I didn't find the line

$myxls->write_string(0,6,get_string("startdate"));

When I export database to excel, the date data appears like 1429704000.

I need to put this line?

Thanks for your attention

In reply to Hilton Sato

Re: Export date to Excel

by Itamar Tzadok -

The grade_export_xls is unrelated to the Database activity export. Currently the Database activity offers no way to export the date in a human readable format. However, after export you can add to the spreadsheet the formula

=(A1/86400)+25569+(-5/24)

where A1 is the cell containing the exported date value (as unix timestamp) in order to convert the date value to date string.

This formula is mentioned in a post down this forum and in many websites around the internet.

hth smile
In reply to Itamar Tzadok

Re: Export date to Excel

by Hilton Sato -
First, thank you for your help. Works great.

Second, sorry. I'm not looking for correctly about the subject.

In reply to Hilton Sato

Re: Export date to Excel

by Itamar Tzadok -

Glad to hear. Nothing to be sorry about. It was just a disclaimer with respect to who deserves the credit for this solution. smile

In reply to Itamar Tzadok

Re: Export date to Excel

by Alexei Hnatiw -

I signed up to Moodle.org simply to tell you how much help this post was.

Been having the same problem for quite some time and am usually terrified to edit the Moodle php files.

Hats off to you sir, worked first time!

In reply to Itamar Tzadok

Re: Export date to Excel

by Web makina -

Hello Itamar, I'm newby @ php sad

Do I have to add this function at the end of the script code (by the line 120 and after the "function get_sort_sql"?

Thank you for your help!

JS

Attachment field-class.jpg
In reply to Web makina

Re: Export date to Excel

by Itamar Tzadok -

It doesn't matter where you put it in the script as long as it is within the class definition as a method of the class. Here's an illustration of the function added at the end of the class:

class data_field_date extends data_field_base {

    var $type = 'date';

    var $day   = 0;
    var $month = 0;
    var $year  = 0;

    function display_add_field($recordid=0) {
        ...
    }

    //Enable the following three functions once core API issues have been addressed.
    function display_search_field($value=0) {
        ...
    }

    function generate_sql($tablealias, $value) {
        ...
    }

    function parse_search_field() {
        ...
    }

    function update_content($recordid, $value, $name='') {
        ...
    }

    function display_browse_field($recordid, $template) {
        ...
    }

    function get_sort_sql($fieldname) {
        ...
    }

    function export_text_value($record) {
        if ($this->text_export_supported()) {
            return userdate($record->content, get_string('strftimedate'), 0);
        }
    }

}

smile

Average of ratings: Useful (1)
In reply to Itamar Tzadok

Re: Export date to Excel

by Lev Abramov -

Hi again Itamar!

Could you post the following information please - separately for each item:

  1. the filename to be hacked
  2. the code to be inserted
  3. the line # where it needs to be inserted
  4. preferably the line content itself, to ensure that the code gets inserted in the right spot

It's been a while since I indulged in code-hacking, so I do not want to make any errors just because I got something wrong.

Thanks in advance -

Lev

In reply to Lev Abramov

Re: Export date to Excel

by Itamar Tzadok -

I did not forget. I should be able to get to that this weekend. smile

In reply to Itamar Tzadok

Re: Export date to Excel

by Lev Abramov -

Great! Sorry about being so impatient (all young children suffer from this need for instant gratification - I am no exception!): the customer keeps pestering me about it a few times a week. Looks like they really need it. sad

Keep in mind that I will probably be using a code editor that supports line numbering, so getting line number indicated would really be helpful, if you can.

Looking forward -

Lev

In reply to Todd Almarez

Re: Export date to Excel

by J C -

Yep, old post, but I found it when looking for the same info. I found a solution.

Here it is.

Excel formula: =(A1/86400)+25569+(-5/24)  where A1 is the cell containing the timestamp. Format your cell to the date of your choice and you're set!

I didn't write it but thought it may help someone else smile

- JC

In reply to J C

Re: Export date to Excel

by Jim Pickens -

Yes, old post, even older reply

Thanks so much for putting this up. Exactly what I was looking for.

Thanks again