later, I can say:
<?php
$today=date("m/d");
$event=Hday[$today];
?>
My question is how do I determine if $event actually captured anything?
Is a statement of the form:
if(strlen($event)>0) go do something
the "simplest" way, or would you recommend some other test?
John Kopf
> can build a sparse array thus:
><?php
> $Hday = array;
> $Hday['07/04']="Happy Forth of July";
> $Hday["12/25']="Merry Christmas";
>?>
>
>later, I can say:
>
><?php
> $today=date("m/d");
> $event=Hday[$today];
>?>
>
>My question is how do I determine if $event actually captured anything?
>Is a statement of the form:
>
> if(strlen($event)>0) go do something
Two notes:
1) Checking the length of a string just to test whether it's empty or
not is unnecessary and quite ugly. A simple $event != '' does the same.
Or call empty() if you also want to allow for NULL values.
2) Here in this example this line
$event=Hday[$today];
will trigger an E_NOTICE error if there's no entry in the array for the
current date. You should check with isset() if there's one:
$today = date('m/d');
$event = isset($Hday[$today]) ? $Hday[$today] : '';
And then:
if ($event != '') {
... // do something with $event
}
You could also do it like this:
$today = date('m/d');
if (isset($Hday[$today])) {
... // do something with $Hday[$today]
}
HTH
Micha
This would still throw a warning because the array key does not exist.
So a cleaner solution might be:
if (array_key_exists($today, $Hday)) { ...
C.
>On 30 Dec, 08:13, Michael Fesser <neti...@gmx.de> wrote:
>> .oO(John Kopf)
>>
>>
>>
>> > �can build a sparse array thus:
>> ><?php
>> > �$Hday = array;
>> > �$Hday['07/04']="Happy Forth of July";
>> > �$Hday["12/25']="Merry Christmas";
>> >?>
><snip>
>>
>> You could also do it like this:
>>
>> � $today = date('m/d');
>> � if (isset($Hday[$today])) {
>> � � �... // do something with $Hday[$today]
>> � }
>>
>
>This would still throw a warning because the array key does not exist.
No problem, isset() can handle this.
>So a cleaner solution might be:
>
>if (array_key_exists($today, $Hday)) { ...
Not necessary here.
Micha
Using empty() can be tricky if one doesn't know what will be evaluated
as true. For example, zero ~ "0" as a string evaluates as true and
it's not empty string or null.
$test = '0';
$test === NULL || $test === '' // false
is not equal as
empty($test) // true
I think this should be mentioned.
***
From: http://php.net/manual/en/function.empty.php
Returns FALSE if var has a non-empty and non-zero value.
The following things are considered to be empty:
* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)
***
I would do something like this:
$event = isset($Hday[$today]) ? $Hday[$today] : FALSE;
if ($event !== FALSE) {
...
}
i.
That works fine (see the result at http://www.scvcma.org/ - within the
next 4 hours - then it disappears again )
Thank yo9u for your kind assistance.
John Kopf
<?php
$Tday = date( "m/d" ); // today
if ( isset( $Hday[ $Tday ] ) )
{
$Event = $Hday[ $Tday ];
echo( "<script><!--\n" );
echo( " HolidayBlock( " . $Event . ")\n" );
echo( "--></script>\n" );
}
?>
...Unfortunately, even though we're located in California, our Server is
in EST; the result is that "$Tday = date( "m/d" ); // today" rolls over
to the next day at 9pm our time.
Is there some easy way to specify I want PST, in the date() call? I
can't seem to find any examples online.
John Kopf
John O. Kopf wrote:
> Michael Fesser wrote:
>> .oO(John Kopf)
>>
>>> SNIO
>>
>> $today = date('m/d');
>> if (isset($Hday[$today])) {
>> ... // do something with $Hday[$today]
>> }
>>
>> HTH
>> Micha
>
> That works fine (see the result at http://www.scvcma.org/ - within the
> next 4 hours - then it disappears again )
>
> Thank you for your kind assistance.
>
> John Kopf
>
> <?php
> $Tday = date( "m/d" ); // today
> Is there some easy way to specify I want PST, in the date() call? I
> can't seem to find any examples online.
Place this as the first line:
date_default_timezone_set("America/Los_Angeles");
--
-bts
-Four wheels carry the body; two wheels move the soul
Better yet - set it in the .htaccess file:
php_value date.timezone "America/Los_Angeles"
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================
I did - got:
Fatal error: Call to undefined function: date_default_timezone_set() in
/nfs/cust/0/66/74/547660/web/NewStuff/M_Main/MainPage.php on line 5
Any suggestions?
John Kopf
What version of PHP is your server using?
http://php.net/manual/en/function.date-default-timezone-set.php
Says: (PHP 5 >= 5.1.0)
Either that, or your path or syntax is incorrect.
John Kopf
The results of the phpinfo() function will tell you.
--
67. No matter how many shorts we have in the system, my guards will be
instructed to treat every surveillance camera malfunction as a
full-scale emergency.
--Peter Anspach's list of things to do as an Evil Overlord
>
> The results of the phpinfo() function will tell you.
php -v
from the prompt.
--
Very old woody beets will never cook tender.
-- Fannie Farmer
... presuming of course that the php that you get from the prompt is the
same php as the one on "the server", and uses the same php.ini... That's
not a situation that's obtained in no small number of funny,
inconsistent problems people have reported here.
--
36. I will not imprison members of the same party in the same cell
block, let alone the same cell. If they are important prisoners, I
will keep the only key to the cell door on my person instead of
handing out copies to every bottom-rung guard in the prison. --EOList
> ... presuming of course that the php that you get from the prompt is the
> same php as the one on "the server", and uses the same php.ini... That's
> not a situation that's obtained in no small number of funny,
> inconsistent problems people have reported here.
True. I tend to use PHP for everything, and I manage to get my host to
install the bits and pieces I rely on, so my test setup is equal to the
public one. I guess not everyone's so lucky.
But the only access I have to the server is FTP (I'm using FileZilla) to
install things, and the browser accessing things installed there - I
can't get to a "prompt".
John Kopf
As Peter said - use phpinfo().
php -v is not necessarily a good idea - it is very possible the command
line version of PHP is different from the one used by the server.
>> "Peter H. Coffin" <hel...@ninehells.com> wrote:
>>> The results of the phpinfo() function will tell you.
>
> But the only access I have to the server is FTP (I'm using FileZilla)
> to install things, and the browser accessing things installed there -
> I can't get to a "prompt".
Create a file named: phpinfo.php
Put the following contents in it:
<?php
phpinfo()
?>
FTP it to your server. Open your browser and access it.
Run it on your home server as well.
Whoops!
> http://example.com/info.php
http://example.com/phpinfo.php
I did this (after several typos :>{ )
got back: "PHP Version 4.4.1"
...so, I guess it's not supported. Is there any way to specify PST in
the date request itself?
John Kopf
You need to upgrade. PHP4.x has not been supported for years now.
>>> Beauregard T. Shagnasty wrote:
>>>>>> Place this as the first line:
>>>>>> date_default_timezone_set("America/Los_Angeles");
>>> Says: (PHP 5 >= 5.1.0)
> [biggasnip]
> I did this (after several typos :>{ )
>
> got back: "PHP Version 4.4.1"
>
> ...so, I guess it's not supported. Is there any way to specify PST in
> the date request itself?
Do some math on the result you get?
Update your server to a modern version of PHP?
Ihave no choice - the server and it's capabilities are *purchased* from
a vendor, who has control over what's there :>{
John Kopf
Unfortunately, it's not "my" server - it's space purchased from an
outside party.
John Kopf
Surely, you mean leased, not purchased -- if you purchased the server, then
you own it, and you can put whatever software on it you damn please.
Even if it's leased, though, you may still be able to use PHP 5. At least one
hosting company I know of has versions 3, 4, and 5 installed; by default, you
get version 4, but you can specify version 3 or 5 with a directive in
htaccess.
If my vendor were so clueless that they are running something that much
out of date, I'd switch vendors IMMEDIATELY. Who knows what other
problems they might have?
I suggest you see if they don't have a supported version available - and
if not, find someone else - ASAP.
As others said: get a better host.
You didn't respond to "Do some math on the result you get." Why don't
you just subtract three hours from the server's time?
Purchase your own server or move to a different outside party. Or at
least threaten to unless they upgrade PHP.
That would involve a fairly complex calculation...
My keys are DATES (e.g., "07/04"), and the date function resolves the
occasional leap year; I can't just subtract 1 if the date comes back as
07/05 and it's between 2100 and 2400; that would screw up other things
like new years (12/31 - last day of the month) or 02/29 (Sadie Hawkins Day)
John Kopf
> Beauregard T. Shagnasty wrote:
>> John O. Kopf wrote:
>>> Beauregard T. Shagnasty wrote:
>>>> John O. Kopf wrote:
>>>>>>> Beauregard T. Shagnasty wrote:
>>>>>>>>>> Place this as the first line:
>>>>>>>>>> date_default_timezone_set("America/Los_Angeles");
>>>>>
>>>>> ...so, I guess it's not supported. Is there any way to specify
>>>>> PST in the date request itself?
>>>>
>>>> Do some math on the result you get? Update your server to a modern
>>>> version of PHP?
>>
>> You didn't respond to "Do some math on the result you get." Why
>> don't you just subtract three hours from the server's time?
>
> That would involve a fairly complex calculation...
Complex? I don't think so.
> My keys are DATES (e.g., "07/04"), and the date function resolves the
> occasional leap year; I can't just subtract 1 if the date comes back
> as 07/05 and it's between 2100 and 2400; that would screw up other
> things like new years (12/31 - last day of the month) or 02/29 (Sadie
> Hawkins Day)
Huh?
You asked how you could change an Eastern Time Zone server's time to
Pacific Time. What's this stuff about leap year and Sadie Hawkins got
to do with subtracting 3 hours from server time?
You said, "...Unfortunately, even though we're located in California,
our Server is in EST; "
Have you never done date math?
$pacific = date("Y-m-d h:i:s", mktime(date("h")-3));
$pacifictime = date("h:i", mktime(date("h")-3));
// Or, if you want to format in the U.S. style:
$pacific = date("m/d", mktime(date("h")-3));
Subtracting three hours is "a fairly complex calculation" ???
>
>My keys are DATES (e.g., "07/04"), and the date function resolves the
>occasional leap year; I can't just subtract 1 if the date comes back as
>07/05 and it's between 2100 and 2400; that would screw up other things
>like new years (12/31 - last day of the month) or 02/29 (Sadie Hawkins Day)
You're waaaaaaaayyyyyyyy overthinking this.
If you'd have been following this thread from the beginning, you'd
realize that your "solution" and rant are completely irrelevant.
Here again is the Problem:
A page will have occasional "obscure Holiday" images and captions
displayed. This can be implemente4d using either PHP or Java. I
decided that doing it in Java would require the client downloading the
entire table of holidays before anything could be displayed; so I chose
to implement it in PHP (Even here in Silicon Valley there are still a
large number of clients connecting through a dial-up modem, and the more
they have to download, the slower the page.)
To that end, I defined:
$Hday = array( );
...and then a number of entries; todays' is:
$Hday[ "01/25" ] = " 'RobertBurns.jpg', '173', '184', 'Happy (Robbie)
Burns Night' ";
...the bopy of the page contains:
<?php
$Tday = date( "m/d" ); // today
if ( isset( $Hday[ $Tday ] ) )
{
$Event = $Hday[ $Tday ];
echo( "<script><!--\n" );
echo( " HolidayBlock( " . $Event . ")\n" );
echo( "--></script>\n" );
}
?>
...this all works fine (You can go to http://www.scvcma.org/ to see the
result).
THE PROBLEM
... is that 'date( "m/d" )' returns the value for the SERVER (which
happens to be located in New Jersey, and thus returns based upon EST).
That means the a CLIENT in California (using PST) will see the result of
$Tday rolling over to the next date at 9PM PST.
Thus, the correction of SERVER time to Client time *is* complex, since
the SERVER has no knowledge of the CLIENT's time zone.
SO, if no one can identify a way for the SERVER to perform the
correction, I see no solution but for the SERVER to download any $Event
for $Tday, as well as any for the day preceding and following, as a
truncatged table (at most 3 entries), and then have Java code on the
CLIENT use the truncated table to set the display for the CLIENT's time
zone.
John Kopf
> Beauregard T. Shagnasty wrote:
>> // Or, if you want to format in the U.S. style:
>> $pacific = date("m/d", mktime(date("h")-3));
>
> If you'd have been following this thread from the beginning, you'd
> realize that your "solution" and rant are completely irrelevant.
Not so, me bucko. ("Rant?" I prefer "succinct.")
> Here again is the Problem:
>
> A page will have occasional "obscure Holiday" images and captions ..
> ...the bopy of the page contains:
> <?php
> $Tday = date( "m/d" ); // today
Repeating (with your variable, instead of $pacific):
$Tday = date("m/d", mktime(date("h")-3)); // today in Pacific time
> THE PROBLEM
> ... is that 'date( "m/d" )' returns the value for the SERVER
UNLESS of course you SUBTRACT the three hours I've been trying to tell
you about...
> happens to be located in New Jersey, and thus returns based upon EST).
> That means the a CLIENT in California (using PST) will see the result of
> $Tday rolling over to the next date at 9PM PST.
No, they won't.
I'm sorry you can't see the simple result. If this above doesn't do it
for you, you're own your own.
> John O. Kopf wrote:
>
>> Beauregard T. Shagnasty wrote:
<snip>
>> Here again is the Problem:
>>
>> A page will have occasional "obscure Holiday" images and
>> captions .. ...the bopy of the page contains:
>> <?php
>> $Tday = date( "m/d" ); // today
>
> Repeating (with your variable, instead of $pacific):
> $Tday = date("m/d", mktime(date("h")-3)); // today in Pacific
> time
You need to use `date('H')', otherwise 1pm EST, for example, will
incorrectly yield yesterday's date. In any case, it's probably
easier to use strtotime():
$tz = 'PST';
/* calculates current timestamp in PST */
$today = date('m/d', strtotime($tz));
If using a numeric offset for the timezone, it'd be more portable
to use `gmdate()' to calculate the timezone offsets with respect
to GMT:
$pst = '-8'; /* PST */
$cet = '+1'; /* CET */
echo gmdate('m/d H:i', strtotime("$pst hours")), "\n";
echo gmdate('m/d H:i', strtotime("$cet hours"));
Both approaches should yield the same results, regardless of the
server's locale.
<snip>
--
Curtis Dyer
<? $x='<? $x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
> You need to use `date('H')', otherwise 1pm EST, for example, will
> incorrectly yield yesterday's date.
Yes, I forgot the 12/24 parameters.
> In any case, it's probably easier to use strtotime():
There are many ways to skin the datetime cat. :-)