Skip to main content
Inspiring
April 27, 2006
Question

PHP Help

  • April 27, 2006
  • 6 replies
  • 386 views
I thought I posted something about this last night, but I'm not seeing it.
http://demo.mediachurch.com/calendarlist.php

I have the following function that takes a Unix timestamp and converts it to
an (excel) serial date



define('EPOCHINEXCEL', 25568);

function priivaUnix2Excel($g) {
return mktime (0, 0, 0, 1, $g + EPOCHINEXCEL, 1970);
}



However, when I call it using this code:



$tester = priivaUnix2Excel($priivaDateToday);
echo "<p> priivaDateToday Unix to Excel: $tester</p>";



It returns a negative number.

I know the value it's getting fed is positive, b/c in the line above it I
have this:

echo "<p> PriivaDateToday(time function): $priivaDateToday </p>";

which is set by this:
$priivaDateToday = time();

and outputs "1146152298 "

So, what my function should be saying is this:

return mktime (0, 0, 0, 1, 1146452298 + 25568, 1970);

How am I ending up with a negative value?!? When I print out the values
fed, they're allshowing positive.

Any help is appreciated.


--

Thank you,

Jon Parkhurst
PriivaWeb
PO Box 1114
Sedalia, MO 65302-1114
660.826.5600


This topic has been closed for replies.

6 replies

Inspiring
April 27, 2006
>> David, I am *so* in love with you.
>
> My wife will be jealous.

ah, she's got to be used to it! LOL.


>> Where should I look up floor?
>
> http://www.php.net/manual/en/function.floor.php

Checking this out - thanks.

>
> If you would like the buy the book (thanks v much), you can find links at
> the bottom right of this page:

I've already got it, thanks mate.

>> It works *perfectly*. Thank you SO MUCH.
>
> My pleasure. I was fascinated by how Excel calculates the date. Once I
> knew that, it was quite simple.
>
> The timestamp created by PHP gives you the number of seconds since 1 Jan
> 1970. Since you want the number of days, divide the number of seconds by
> 60 (to get minutes), then by another 60 (to get hours), and finally by 24
> (to get days). The floor() function simply rounds it down to the nearest
> integer. All that's needed after that is to add the Excel equivalent of 1
> Jan 1970.

Thank you so much for this very simple explanation. I don't know how to
explain how greatful I am. I feel like I've been tryign to explain
something in a foreign language I don't really get when I'm only 1/2 sure of
what I want. (I guess which is exactly what I'm doing). This date stuff
has been very hard to grasp because it seems like every time I think I've
got my head around it, I find another function that handles it completely
differently - and works!

Really, I don't know how to express how thankful to everybody that has
helped me on this. I have not had this level of frustration since potty
training!

If I ever make it to a conference, I'm definitely buying the PHP table a
night of drinks.


Inspiring
April 27, 2006
crash wrote:
> David, I am *so* in love with you.

My wife will be jealous.

> Where should I look up floor?

http://www.php.net/manual/en/function.floor.php

> gimme the link you want me to buy your book from. it's definitely worth
> this answer. (found it on your site)

If you would like the buy the book (thanks v much), you can find links
at the bottom right of this page:

http://foundationphp.com/dreamweaver8/index.php

> It works *perfectly*. Thank you SO MUCH.

My pleasure. I was fascinated by how Excel calculates the date. Once I
knew that, it was quite simple.

The timestamp created by PHP gives you the number of seconds since 1 Jan
1970. Since you want the number of days, divide the number of seconds by
60 (to get minutes), then by another 60 (to get hours), and finally by
24 (to get days). The floor() function simply rounds it down to the
nearest integer. All that's needed after that is to add the Excel
equivalent of 1 Jan 1970.

--
David Powers
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "Foundation PHP 5 for Flash" (friends of ED)
http://foundationphp.com/
Inspiring
April 27, 2006
David, I am *so* in love with you.

Where should I look up floor? God - there's SO MANY SIMILAR THINGS IN PHP.
Wow.

gimme the link you want me to buy your book from. it's definitely worth
this answer. (found it on your site)

It works *perfectly*. Thank you SO MUCH.

"David Powers" <david@example.com> wrote in message
news:e2qt7i$gp5$1@forums.macromedia.com...
> crash wrote:
>> I have the following function that takes a Unix timestamp and converts it
>> to an (excel) serial date
>
> Try this:
>
> function dpUnix2Excel($timestamp) {
> return floor($timestamp/60/60/24) + EPOCHINEXCEL;
> }
>
> --
> David Powers
> Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
> Author, "Foundation PHP 5 for Flash" (friends of ED)
> http://foundationphp.com/


Inspiring
April 27, 2006
Thanks Joe, I"m struggling to understand this.


> http://www.php.net/manual/en/function.mktime.php

I've read and re-read this. I"m afraid I'm still not correctly
understanding it.

> The fourth field of mktime() takes day of month as an argument, so it
> would be in the range [1-31], [1-30], [1-28] or [1-29], depending on the
> month and year. If you set the function up as

OK, so im my example, it's 1.

>
> $myUnixTimestamp = mktime(0,0,0,4,27,2006);
>
> $myUnixDatestamp now contains the Unix timestamp for today (1146110400).

I follow you here, but am missing the connection. Should my mktime function
contain more variables?

> Excel thinks today is day 38834 (1/1/1900 is day 1). The conversion is
> left to the reader...

I don't know what you mean by the conversion is left to the reader, but I'm
guessing by your .... you're saying something should be obvious?

I tlooks like David might have solved my problem.

Sorry for being so dense. Dates are so hard for me to work with.


Inspiring
April 27, 2006
crash wrote:
> I have the following function that takes a Unix timestamp and converts it to
> an (excel) serial date

Try this:

function dpUnix2Excel($timestamp) {
return floor($timestamp/60/60/24) + EPOCHINEXCEL;
}

--
David Powers
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "Foundation PHP 5 for Flash" (friends of ED)
http://foundationphp.com/
Inspiring
April 27, 2006
On Thu 27 Apr 2006 11:50:33a, crash wrote in
macromedia.dreamweaver.appdev:

> I have the following function that takes a Unix timestamp and converts
> it to an (excel) serial date
>
> define('EPOCHINEXCEL', 25568);
>
> function priivaUnix2Excel($g) {
> return mktime (0, 0, 0, 1, $g + EPOCHINEXCEL, 1970);
> }
>
> However, when I call it using this code:
>
> $tester = priivaUnix2Excel($priivaDateToday);
> echo "<p> priivaDateToday Unix to Excel: $tester</p>";
>
> It returns a negative number.
>
> I know the value it's getting fed is positive, b/c in the line above
> it I have this:
>
> echo "<p> PriivaDateToday(time function): $priivaDateToday </p>";
>
> which is set by this:
> $priivaDateToday = time();
>
> and outputs "1146152298 "
>
> So, what my function should be saying is this:
>
> return mktime (0, 0, 0, 1, 1146452298 + 25568, 1970);
>
> How am I ending up with a negative value?!? When I print out the
> values fed, they're allshowing positive.

http://www.php.net/manual/en/function.mktime.php

The fourth field of mktime() takes day of month as an argument, so it
would be in the range [1-31], [1-30], [1-28] or [1-29], depending on the
month and year. If you set the function up as

$myUnixTimestamp = mktime(0,0,0,4,27,2006);

$myUnixDatestamp now contains the Unix timestamp for today (1146110400).
Excel thinks today is day 38834 (1/1/1900 is day 1). The conversion is
left to the reader...