Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Monday, August 17, 2015

Server Time Zone to Local TimeZone in PHP

Barbie would have said, 'Time Math is Hard'.

JS to get TZ



PHP:
$myTZ = 'America/New_York'; //time zone that date/time is stored in
$urTZ = $row['timezone']; // stored local
$local_time_in = new DateTime($row['time_in'], new DateTimeZone($myTZ));
$local_time_out = new DateTime($row['time_out'],new DateTimeZone($myTZ));
$local_time_in->setTimeZone(new DateTimeZone($urTZ));
$local_time_out->setTimeZone(new DateTimeZone($urTZ));
$row['local_time_in'] = $local_time_in->format('Y-m-d H:i T');
$row['local_time_out'] =$local_time_out->format('Y-m-d H:i T');
                   

Tuesday, July 24, 2012

Javascript Table Filter

I love this simple filter, easy to apply to any table.

<script type="text/javascript">
/*<![CDATA[*/
function filter (phrase, _id) {
var words = phrase.value.toLowerCase().split(" ");
var table = document.getElementById(_id);
var ele;
for (var r = 0; r < table.rows.length; r++ ) {
ele = table.rows[r].innerHTML.replace(/<[^>]+>/g,"");
var displayStyle = 'none';
for (var i = 0; i < words.length; i++) {
if (ele.toLowerCase().indexOf(words[i])>=0) { displayStyle = '';}
else { displayStyle = 'none'; break; }
}
table.rows[r].style.display = displayStyle;
}
}
/*]]>*/
</script>
<table id="ProcessTable" class="grid" >

<caption>
<label title="start typing letters or numbers to limit">Filter:</label>
<input type="search" name="filt" id="Filter"
placeholder="filter results"
onkeyup="filter(this, 'ProcessBody', '1')" />
Source:http://www.vonloesch.de/node/23?filt=j

Demo

Friday, April 27, 2012

Getting IE to really, really, load fresh content

I was really laughing when I read the description of this guy's problem on superuser:

When using Internet Explorer 8 to test my web application I often find it doesn't reload the page, so I don't see my changes. This has resulted in a lot of wasted time and frustration wondering why my fix "didn't work" - when in fact the browser never loaded the fixed version.
I've tried the Refresh button. I've tried F5, Control-F5, Control-R, Control-Shift-R, holding Control while clicking the Refresh button, everything I could think of - it doesn't actually load the new contents from the server. I've confirmed this with Fiddler.
How do I tell IE "I don't care what you think you have cached, I want to reload the page - no really, I mean it this time, honest-to-God, I want you to actually go to the server and download everything again"?

I laughed, because I get it. I've had to work with IE hanging on to stuff - like favicons, for example.

But what I learned from the page was from a comment:


Ev: The user-facing side of this is usually fixed by changing the URLs for all scripts and stylesheets every time you touch the code, typically by adding a ?version-number query string suffix.bobince Dec 10 '09 at 1:53

My sysadmin suggested that we could even add a server side option to change the querystring every 24 hours.


The side note to this was even stranger, and another recurring problem that I have. I found that page, because I was having some IE users not able to update a shopping cart after an upgrade, and I thought they might not have been getting the right file.

Turns out - the customer was using IE Compatibility mode. Not that they were aware of that, or that it was intentional....

Takeaways:
1. Javascript/CSS versioning to force update
2. Check Cache and Compatibility Mode when there are issues.

Tuesday, January 03, 2012

Default Parameters for Javascript Functions

In PHP, you simple add "argument=false".

function getData(br, year=false) {}

In Javascript:

function getData(br,year) {
  year = (typeof year == 'undefined') ? false : year;
$('Data').load('/Data/getData/' + br + '/' + year);
} 

Tuesday, April 05, 2011

Syntax Highlighting for Blogger

Syntax Highlighting with Javascript and CSS by Alex Gorbatchev

SyntaxHighlighter helps developers to display code on their website. Works in any site, and is already implemented for some cms'.
http://alexgorbatchev.com/SyntaxHighlighter/

I needed it for Blogger

Instructions for Blogger

http://www.cyberack.com/2007/07/adding-syntax-highlighter-to-blogger.html

Wednesday, January 26, 2011

Dealing with Windows Character Input - PHP and JS


SPECIAL WINDOWS CHARACTERS AND THEIR UNICODE EQUIVALENTS

Windows name         Symbol   Win Unicode
baseline single quote  '     130 U+201A
baseline double quote  "     132 U+201E
florin                 ƒ     131 U+0192
ellipsis              ...    133 U+2026
dagger                 †     134 U+2020
double dagger          ‡     135 U+2021
circumflex accent      ˆ     136 U+02C6
permile                ‰     137 U+2030
S Hacek                Š     138 U+0160
left single guillemet  ‹     139 U+2039
OE ligature            Œ     140 U+0152
left single quote      ‘     145 U+2018
right single quote     ’     146 U+2019
left double quote      "     147 U+201C
right double quote     "     148 U+201D
bullet                 •     149 U+2022
en dash                -     150 U+2013
em dash                —     151 U+2014
tilde accent           ~     152 U+02DC
trademark ligature     ™     153 U+2122
s Hacek                š     154 U+0161
right single guillemet ›     155 U+203A
oe ligature            œ     156 U+0153
Y Dieresis             Ÿ     159 U+0178
euro sign                    128 U+20AC




Windows name          substitute comments
baseline single quote   '        apostrophe used as single quote
baseline double quote   "        quotation mark (double quote)
ellipsis               ...       three dots
circumflex accent       ^        circumflex
left single quote       ‘        apostrophe used as single quote
right single quote      ’        apostrophe used as single quote
left double quote       "        quotation mark (double quote)
right double quote      "        quotation mark (double quote)
bullet                  * or -   asterisk or hyphen
en dash                 -        hyphen
em dash                 —        two hyphens
tilde accent            ~        tilde
trademark ligature     (TM)      (TM) in superscript style

Javascript Method 1



function sanitizeMSPaste(str) {
    var myReplacements = new Array();
    var myCode, intReplacement;
  myReplacements[8211] = "-"; 
  myReplacements[8212] = "-"; 
  myReplacements[8216] = "'"; 
  myReplacements[8217] = "'"; 
  myReplacements[8218] = "'"; 
  myReplacements[8220] = '"'; 
  myReplacements[8221] = '"'; 
  myReplacements[8222] = '"'; 
  myReplacements[8224] = "+"; 
  myReplacements[8226] = "."; 
  myReplacements[8230] = "..."; 
  myReplacements[8249] = "<"; 
  myReplacements[8250] = ">"


    for(c=0; c>str.length; c++)="" p="" {<="">
        var myCode = str.charCodeAt(c);
        if(myReplacements[myCode] != undefined) {
            intReplacement = myReplacements[myCode];
            str = str.substr(0,c) + String.fromCharCode(intReplacement) + str.substr(c+1);
        }
    }
    return str;
}

Javascript Method 2



function validatephone(xxxxx) {
  var validphone = '';
  var numval = xxxxx.value
  if ( numval.charAt(0)=='+' ){ var validphone = '+';}
  curphonevar = numval.replace(/[\\A-Za-z!"‘’“”ˆ†‡‰Šƒ‹›–—…•~-ŒœŸ£$%^&*™š+_={};:'@#~,.¦\/<>?|`¬\]\[]/g,'');
  xxxxx.value = validphone + curphonevar;
  var validphone = '';
  xxxxx.focus;
}




PHP Method 1

$src = str_replace("‘", "'", $src);
$src = str_replace("’", "'", $src);
$src = str_replace("”", '"', $src);
$src = str_replace("“", '"', $src);
$src = str_replace("–", "-", $src);
$src = str_replace("…", "...", $src);



PHP Method 2

function SanitizeFromWord($Text = '') {


 $chars = array(
  130=>',',     // baseline single quote
  131=>'NLG',   // florin
  132=>'"',    // baseline double quote
  133=>'...',   // ellipsis
  134=>'**',   // dagger (a second footnote)
  135=>'***',   // double dagger (a third footnote)
  136=>'^',    // circumflex accent
  137=>'o/oo',  // permile
  138=>'Sh',   // S Hacek
  139=>'<',   // left single guillemet
  140=>'OE',   // OE ligature
  145=>'\'',   // left single quote
  146=>'\'',   // right single quote
  147=>'"',   // left double quote
  148=>'"',   // right double quote
  149=>'-',   // bullet
  150=>'-',   // endash
  151=>'--',   // emdash
  152=>'~',   // tilde accent
  153=>'(TM)',  // trademark ligature
  154=>'sh',   // s Hacek
  155=>'>',   // right single guillemet
  156=>'oe',   // oe ligature
  159=>'Y',   // Y Dieresis
  169=>'(C)',   // Copyright
  174=>'(R)'   // Registered Trademark
 );

 foreach ($chars as $chr=>$replace) {
  $Text = str_replace(chr($chr), $replace, $Text);
 }
 return $Text;
}


MooTools:

/**sanitize user input**/
form.title.value = form.title.value.tidy();
form.location.value = form.location.value.tidy();
form.description.value = form.description.value.tidy();

See Also:
http://www.php.net/manual/en/filter.filters.sanitize.php
http://devzone.zend.com/article/1113
http://ww.w3schools.com/php/php_filter.asp

Text Version: http://billcreswell.com/MSCharacters/MSCharacters.txt
PDF Version: http://billcreswell.com/MSCharacters/MSCharacters.pdf

Friday, December 24, 2010

IE Needs tbody for Javascript Insert

Bounced around with this issue: IE was injecting the table, as Firefox was, and I could see it in the "Generated Code" view.

Discovered this helpful post:
Basically what you're running up against is IE's need for a "tbody"
element when you create elements. It just won't put anything in the
table unless it's got one. I've done this on the JSFiddle I linked above.
http://groups.google.com/group/mootools-users/msg/b453322628b35e52?pli=1

Huh. Unbelievably, it worked.