Date format in float from Persavise db

329 Views Asked by At

I got data from a Persavive V12 Db. The dates are in a weird format. They are like float :

Example of date in table : 39787.0

Do someone know this kind of format ? It seems to be the number of days from 1901-01-01.

Since the dates are float, is it possible to add days from a date in PHP ? Like :

$from = '1901-01-01';
$date = date('Y-m-d', strtotime($from .' +'. $float .' days'));

I got always 1970-01-01. Is there another way ?

2

There are 2 best solutions below

1
On

My first answer so don't hurt me much. I'm not quite sure what you want to do (put back in the database or perform further operations so I only posted one solution)

The number you posted 39787.0 is a unix timestamp. You need to use mktime to convert what you want to a number (And you need date to convert it to a readable form). Since mktime uses 1901-01-01 as a starting date as well you have to make a couple of changes.

I was a biz lazy and didn't include all the code for all fields but this should allow you modify to add whatever you want (and possibly clean it up to a single line if you want). I made it in long form so it's easy to read.

    <?

    $olddate = 39787.0;
    $hour = 0;
    $min =  0;
    $sec =  0;
    $month =  0;
    $year = 0;
    $day = 12;
    $month += date("m",$olddate);
    $day += date("d",$olddate);
    $year += date("Y",$olddate);
    $x = date("Ymd",mktime($hour,$min,$sec,$month,$day,$year));
    echo $x;

    ?>
0
On

If 39787.0 is in the database, what value is that in a real date (using the application)? If it's 2008-12-05, the it's a VB Date. Do you know what the application was written in? I've seen people use the VB Date data type and store it in an 8 byte float in Btrieve (back in 2003). If it's a VB Date, 1 is 1899-12-31. You should be able to add the number to 1899-12-30 to get the correct date value. Something like:

<?php
$date = new DateTime('1899-12-30');
$date->add(new DateInterval('P39787D'));
echo $date->format('Y-m-d') . "\n";
?>