Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace OpenSpout\Writer\XLSX\Helper;
6
 
7
use DateInterval;
8
 
9
/**
10
 * @internal
11
 */
12
final class DateIntervalHelper
13
{
14
    /**
15
     * Excel stores time durations as fractions of days:
16
     *   A value of 1 equals 24 hours, a value of 0.5 equals 12 hours, etc.
17
     *
18
     * Note: Excel can only display durations up to hours and it will only auto-detect this value as an actual duration
19
     *     if the value is less than 1, even if you specify a custom format such als "hh:mm:ss".
20
     *   To force the display into a duration format, you have to use the brackets around the left most unit
21
     *     of the format, e.g. "[h]:mm" or "[mm]:ss", which tells Excel to use up all the remaining time exceeding
22
     *     this unit and put it in this last unit.
23
     */
24
    public static function toExcel(DateInterval $interval): float
25
    {
26
        // For years and months we can only use the respective average of days here - this won't be accurate, but the
27
        // DateInterval doesn't give us more details on those:
28
        $days = $interval->y * 365.25
29
            + $interval->m * 30.437
30
            + $interval->d
31
            + $interval->h / 24
32
            + $interval->i / 24 / 60
33
            + $interval->s / 24 / 60 / 60;
34
 
35
        if (1 === $interval->invert) {
36
            $days *= -1;
37
        }
38
 
39
        return $days;
40
    }
41
}