0

I have this date value being stored into my database from a VBScript application that I need to read out and generate a report on in PHP. The problem I have is converting that date value into a date value in PHP but I can't seem to get anything meaningful.

The value being store from VBScript is Now()*24*60*60 and the actual number it generates is '3603612890' and the approximate time it should equal is '2014/03/10 11:35:15', give or take a few seconds.

How do I convert this number '3603612890' to equal this value '2014/03/10 11:35:15' in PHP

Thanks

Tw1tCh
  • 89
  • 1
  • 12

4 Answers4

0

Try this:

date("Y-m-d H:i:s", 3603612890);

To convert the other way, you want strtotime()

Nathan Rice
  • 3,091
  • 1
  • 20
  • 30
  • I tried that and gmdate but the value returned is '2084-03-11 06:34:50', 70+ years into the future. what I expect to see is something closer to '2014/03/10 11:35:15' – Tw1tCh Mar 10 '14 at 16:54
  • Is it consistently the same value difference? You could always just do the math to remove the difference if it's the same. Calculate the difference and display it and see if it remains the same. – Nathan Rice Mar 10 '14 at 16:59
  • It does seem consistent with all the values and I could likely try and workout the difference but I need these values to be accurate to the second. I'd prefer to take any guesswork out of the equation. – Tw1tCh Mar 10 '14 at 17:28
  • 1
    Take a known date and work out the value. Should be really simple. – Nathan Rice Mar 10 '14 at 17:36
  • 1
    http://stackoverflow.com/questions/5471379/ways-to-convert-unix-linux-time-to-windows-time – Nathan Rice Mar 10 '14 at 17:48
  • If you are unwilling to update the code. Nathan's answer is the way to go. – Rich Mar 10 '14 at 18:49
0

Store your time off of the unix epoch with vbscript like the following:

VBScript

DateDiff("s", "01/01/1970 00:00:00", Now())

Then with your PHP. First set your Timezone, then convert your integer to the appropriate date/time so it's accurate to the second.

PHP

date_default_timezone_set('UTC');
$orig = 1394454630;
$dbTime = date("Y-m-d H:i:s", $orig);
echo $dbTime;

PHPFiddle

After you have applied the unix epoch base from vbscript and counted the seconds, PHP's default date() function converts off of the unix epoch by default. So, by adding in the additional timezone_set, you get the exact time converted from vbs to php.

Rich
  • 4,134
  • 3
  • 26
  • 45
  • I can't change the way this Date is being stored at this point, this is currently in production for several years, changing it now would render all the previous data useless. – Tw1tCh Mar 10 '14 at 17:49
0

The error in the existing VBScript function is the calculation to seconds. I do not know why this was done, but you have to calculate this back to a VBScript date to get a workable date:

3603612890 / (24 * 60 * 60)

this results in 41708.4825...
This means "somewhat before 12:00". In VBScript, 0.5 in a date is exactly noon, 0.25 is exactly 6:00 etc. But because 24*60*60 = 86,400 and not 100,000; you cannot say 41708.00060 is one minute past 0:00, or '2014/03/10 00:01:00', no actually it is '2014/03/10 00:00:52'

If you have VBScript at your use, you can get the Unix epoch by the amount of seconds since 01/01/1970 to the given date:

date2epoch = DateDiff("s", "01/01/1970 00:00:00", myDate)

This results in: 1394451290 for the given date.

If you only have PHP (which I am not accustomed with) you can get the day by taking the integer part, that is the amount of days since 30-12-1899 (don't ask why, this has a strange history).
The amount of seconds since 00:00:00 is the decimal part * 86400

AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
0

Thanks everyone who shared their input.

It turns out that there is a very simple way to convert these dates consistently.

First you have to undo Now()*24*60*60 and go back to the original VBScript Date value of 41708.48252314815 then there is this magical number, some base date constant, in VBScript which equals to 25569 that you subtract from the number above and then multiply the total by 86400 again to get your time in seconds from 1970-01-01 00:00:00 which works well in PHP.

The formula looks as such:

gmdate("Y-m-d H:i:s", ((3603612890/86400)-25569) * 86400));
Tw1tCh
  • 89
  • 1
  • 12