UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

537 lines (394 loc) 16.3 kB
<!-- Generated by Rakefile:build --> <strong> Matteo </strong> on 2012-07-09 17:49:26 <br /> I do not know if this can help, but I noticed this: date('Y-m-d', strtotime('2012-07-01 +7 DAY')) = 2012-07-11 --&gt; wrong but date('Y-m-d', strtotime('+7 DAY', strtotime('2012-07-01'))) = 2012-07-08 --&gt; correct <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-07-05 03:42:23 <br /> @Matteo: Yeah, I see. Based on other comments here as well, this function needs a good amount of work, and I'm a bit busy to undertake it now. Anyone? Looking at PHP source or http://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html would be ideal to ensure all areas are addressed. <hr /> <strong> Matteo </strong> on 2012-07-04 16:58:34 <br /> @Brett: I do not think that the timezone can justify a difference of many days! using php.js: date('Y-m-d', strtotime('2012-07-01 +7 DAY')) = 2012-07-11 --&gt; wrong using php: date('Y-m-d', strtotime('2012-07-01 +7 DAY')) = '2012-07-08' --&gt; correct <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-07-04 14:52:48 <br /> @Tom: Are you using the latest versions of the code? Please visit &quot;raw js source&quot;. I am getting the result you said you were expecting. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-07-04 14:47:12 <br /> @wookie: I made a change; are u using Adobe Air, I think it was which passes around String objects for some reason? @Daniele: You can make a &quot;pull request&quot; at Github for us to accept your changes. Everyone else commenting here: Sorry, moving slowly here. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-07-04 02:28:54 <br /> @Matteo: I haven't looked at it carefully, but are you controlling for timezone (browser vs. server)? <hr /> <strong> Matteo </strong> on 2012-07-03 19:15:07 <br /> There is a problem: using this function: strtotime('2012-07-01 +7 DAY') = 1341931365.248 while PHP says: strtotime('2012-07-01 +7 DAY') = 1341698400 is it an error on my part or in the function? <hr /> <strong> wookie </strong> on 2012-06-15 10:25:32 <br /> I was getting an error when I was sending '2004-02-12T23:00:00.000Z' to get converted, it complained that it wasn't a string.. weird. Anyway my fix was: <pre><code> strTmp = str+''; </code></pre> <hr /> <strong> <a href="wardhana.com" rel="nofollow">Denny Wardhana</a> </strong> on 2012-03-04 03:30:41 <br /> I've just noticed that this function and also other functions which are using Date.parse return incorrect result in: - Chrome 4 and older (can be ignored) - IE8 and older (hardly to be ignored) - Safari latest version and older (shouldn't be ignored) <hr /> <strong> Tom </strong> on 2011-12-28 06:38:32 <br /> echo(date(&quot;Y-m-d H:i:s&quot;, strtotime(&quot;last monday&quot;, 1325050486))); gives: 2011-11-28 06:34:46 expected: 2011-12-26 00:00:00 <hr /> <strong> <a href="www.plancake.com" rel="nofollow">Daniele</a> </strong> on 2011-10-11 10:47:48 <br /> I am using this function on something mission-critical for the user (an online task manager and calendar) so I need it to be flawless. I am trying to find bugs and fix them myself. If you want to help me, please contact me. I would also like to contribute to the Github repo with my fixes - how can I do that? Here is another bug: date(&quot;Y-m-d&quot;, strtotime('+3 days')); // returns 2011-10-14 date(&quot;Y-m-d&quot;, strtotime('+3 days', 1324815132)); // returns 2011-10-14 I have fixed the bug by replacing line 30 of the version version: 1109.2015 with: } else if ((!now) &amp;&amp; !isNaN(parse = Date.parse(strTmp))) { I hope that won't introduce new bugs. Any ideas? <hr /> <strong> <a href="http://www.plancake.com" rel="nofollow">Daniele</a> </strong> on 2011-10-11 00:20:00 <br /> This function would be so useful if it was reliable An example: PHP date(&quot;Y-m-d&quot;, strtotime('second Monday October 2011')); returns: 2011-10-10 (which is right) JS: date(&quot;Y-m-d&quot;, strtotime('second Monday October 2011')); returns: 1970-01-01 (which is obviously wrong) Any solution, suggestion, work around? Is this function still maintained? If not, maybe I can start working with somebody to maintain it back. <hr /> <strong> Brad Ramsey </strong> on 2011-10-04 07:10:20 <br /> I don't understand why 2011-10-03 is not the same as 10/03/2011... so I patched the function after line 25: <pre><code> match = strTmp.match(/^(\d{4})-(\d{2})-(\d{2})$/); if (match != null) { strTmp = match[2] + '/' + match[3] + '/' + match[1]; } <pre><code> Worked for me. <hr /> <strong> Romaric </strong> on 2011-08-08 14:50:27 <br /> Hi everybody Nice function, I've used it a lot, but encountered a few issues : - it seems unable to decode YYYYMMDD format (not really bad, just use a regex) - in French, there are accented chars in month names. But when using short names, it will truncate the html char, i.e. AO&amp; instead of AO&amp;Ucirc;. I came up with this fix, using html_entity_decode (you may want to apply it to week days, too) : <pre><code> M: function () { // Shorthand month name; Jan...Dec // Romaric : using html_entity_decode to avoid bugs with accented chars var _month = f.F(); _month = html_entity_decode(_month); return _month.slice(0, 3); }, </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-07-28 20:32:01 <br /> Yeah. Date.parse() is now apparently treating &quot;11&quot; as 1911. And since Date.parse() might vary across browsers, we should I think be using our own implementation anyways rather than allowing for different behavior by browser. Thoughts? <hr /> <strong> <a href="http://jasonbutz.info" rel="nofollow">Jason</a> </strong> on 2011-07-28 17:37:51 <br /> Try this in Firefox 5: <pre><code> console.log(strtotime(&quot;06/28/2011&quot;)); console.log(strtotime(&quot;06/28/11&quot;)); </code></pre> You get <pre><code> 1309237200 -1846522800 </code></pre> It isn't properly detecting 2 digit years. <hr /> <strong> <a href="http://zubink.com" rel="nofollow">Zubin Khavarian</a> </strong> on 2011-06-24 22:28:45 <br /> There seems to be a bug where different date formats return different values, try: strtotime('2011-06-24'); //1308873600 strtotime('06/24/2011'); //1308891600 date('D M j, Y', strtotime('2011-06-24')); //Thu Jun 23, 2011 date('D M j, Y', strtotime('06/24/2011')); //Fri Jun 24, 2011 <hr /> <strong> <a href="http://scott.connerly.net" rel="nofollow">Scott Connerly</a> </strong> on 2010-12-09 01:43:34 <br /> When parsing ISO 8601 dates, PHP's strtotime() doesn't care if there's a colon in the GMT-06:00. php.js's version requires the colon to be there. This most notably comes into play with Facebook's open social graph dates, it won't parse them unless you insert the colon. <hr /> <strong> <a href="empatix.no" rel="nofollow">Tommy</a> </strong> on 2010-11-10 13:44:41 <br /> hello, this function doesnt support format like this: 2010-11-10T13:34:00+01:00 <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-08-29 05:58:29 <br /> @Brad: I think it is most likely a timezone issue. Our date function currently does not support the setting of a default timezone (as PHP also allows and recommends), so there may be some problem in setting date values according to the browser locale and then using such values across our functions. You can see &quot;raw js source&quot;, note #2, on the date function for the latest notes about how this could be implemented if you are able to make the changes to our code yourself (sorry, I have no time at the moment). Anyone else feel free to help out if you can. <hr /> <strong> Brad </strong> on 2010-08-28 16:25:16 <br /> When I run <pre><code> date(&quot;F j, Y&quot;, strtotime(&quot;2010-08-03&quot;)); </code></pre> I get &quot;August 2, 2010&quot; <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2010-08-17 14:48:50 <br /> test <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-08-16 15:04:07 <br /> @Jack: The syntax highlighting code here is buggy, so when you copy-paste it directly, some lines will be commented out. Go to &quot;raw js source&quot; to make sure you have a good copy...It is working with the right code... <hr /> <strong> Jack </strong> on 2010-08-16 14:34:11 <br /> Example 4 returns NaN. Seems to be broken! <hr /> <strong> Mike </strong> on 2010-04-02 18:29:06 <br /> hmm - couple of bugs in strtotime... 1) strtotime('last Monday',now) returns last month not last monday - first 3 letters of month is the same as monday! work around : <pre><code> strtotime('last Tuesday, -1 day',now); // = last monday </code></pre> 2) strtotime('d-m-Y',now) isn't handled correctly (only 'Y-m-d' is) (as mentioned in other comments) 3) in 2 above, the d and m have to be zero padded to work I.E. strtotime('2010-3-1') has to be replaced with ('2010-03-01') work around : <pre><code> //examples from variable passed into function perhaps day = 2; month = 5; year = 2010; if (day &lt; 10) dayzeropad = '0'; else dayzeropad = ''; if (month &lt; 10) monthzeropad = '0'; else monthzeropad = ''; var tstring = year + '-' + monthzeropad + month + '-' + dayzeropad + day; unixtime = strtotime(tstring); </code></pre> bugs work-aroundable... cool stuff - thanks. <hr /> <strong> Zahlii </strong> on 2010-03-10 20:31:33 <br /> Is it possible to add support for german-like Dates ? in PHP (5.3+), strototime('22.01.2010'); works as expected, but not with this js strtotime-method. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-12-14 15:28:53 <br /> @ rav3n: You might be better off parsing your string first before feeding it to strtotime cause I believe PHP wouldn't support your format either. @ iwosz: Sorry no IE7 on my machine. So unless someone else can get to the bottom of this I may have to see if I can find some WinXP virtual image with IE7 installed or sth like that <hr /> <strong> iwosz </strong> on 2009-12-09 12:46:54 <br /> Under IE 7 strtotime('2009-12-09 12:45:16') returns NaN, can you explain that? some solutions? thx. <hr /> <strong> rav3n </strong> on 2009-12-01 10:34:34 <br /> this function is great. however i need to format the string not as %Y-%m-%d but as %d-%m-%Y. i understand that it's around line 150-170 <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-09 10:31:28 <br /> @ Daniel Janesch &amp; Theriault: Thanks for pointing out these problems. I've contacted the original author by mail. <hr /> <strong> <a href="http://www.fotoeck.at" rel="nofollow">Daniel Janesch</a> </strong> on 2009-10-08 22:10:13 <br /> First of all: Great work :-) I have figured out that the following is not working correctly: <pre><code> $d = new Date(2012, 11, 1, 7, 0, 0); $r = strtotime('3 Wednesday', $d.getTime()/1000); // 2012-11-01 07:00:00 </code></pre> The correct result with php is 2012-11-21 00:00:00 but with phpjs I get 1351753200 wich is 2012-11-01 07:00:00. Any clues? <hr /> <strong> Theriault </strong> on 2009-10-06 00:18:18 <br /> PHP seems to apply the string as a whole and not from left-to-right as in this function. In the following PHP example, both $a and $b will be the same date. If PHP applied them from left-to-right, $b would be 2012-03-01, not 2012-02-29. <pre><code> $d = strtotime('2011-02-22'); $a = strtotime('+1 year, +7 days', $d); // 2012-02-29 $b= strtotime('+7 days, +1 year', $d); // 2012-02-29 </code></pre> This function applies the string from left-to-right, which seems more logical, but is incorrect. <hr /> <strong> Theriault </strong> on 2009-10-04 02:46:35 <br /> I noticed that none of the following work with this function: today (same as 'now' but resets to 12:00:00AM), tomorrow (same as '+1 day' but also resets to 12), yesterday (same as '-1 day' but also resets to 12). next week, last week (Currently returns date plus/minus seven days, should return next monday if I remember correctly on how it works in PHP). last sunday of next month (returns next month, but not last sun/sat/mon/day... of that month) YYYY-Wnn-d (Basically, sets year to YYYY, week number to nn, and then day of that week to d (0-6). Day of week may be omitted and also overflow, such as 7 for next week.) 00:00:00 (Changes time to 12:00:00 AM or anything else entered here.) Does anyone else see anything missing from this function? I may recode it. I would like to see it complete -- it is a great function. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-08-17 15:39:58 <br /> @majak: Yeah, thanks, it seemed that way from the code snippet (figured out what was the problem by finding that code by Google). Be careful using Prototype or other libraries that overload the prototype objects--it causes problems like these... <hr /> <strong> majak </strong> on 2009-08-17 14:12:36 <br /> @Brett Zamir: Thank you for your help. Your fix works. Just for reference, I'm using Prototype framework. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-08-12 14:49:55 <br /> @majak, it appears you are using a library which overloads the Object prototype with its own functions which this PHP.JS function then iterates... You can solve this problem by adding right after line 184 (see above) the following line (and then the closing brace after line 187): <pre><code>if (match.hasOwnProperty(i)) {</code></pre> If you need to work with older browsers like IE 5, you can add this instead: <pre><code>if (typeof match[i] !== 'function') {</code></pre> See http://yuiblog.com/blog/2006/09/26/for-in-intrigue/ for a fuller explanation of why. All of our for...in statements should really use such a test, though since it is cumbersome, and libraries should, imo, really not be altering the prototype in the first place, I think it is a toss-up as to whether we should add these checks ourselves. What do you think, Kevin? Perhaps we should in order to be most compatible... <hr /> <strong> majak </strong> on 2009-08-12 11:05:40 <br /> I'm using the latest (2.85) minified and namespaced version of this function. This snippet: <pre><code> $P.strtotime('+1 day'); </code></pre> produces TypeError. I have tested it in both FF and Chrome, here are their error messages: FF: TypeError: match[i].split is not a function Chrome: TypeError: Object function (inline) { return (inline !== false ? this : this.toArray())._reverse(); } has no method 'split' I suppose it happens on every relative date string input, like &quot;-3 months&quot;. <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-06-27 02:16:40 <br /> The function expects a string--you are mostly passing something to the function which is not a string. <hr /> <strong> Andrew </strong> on 2009-06-27 01:02:57 <br /> Getting an error -- <pre><code>Error: strTmp.replace is not a function</code></pre> The debugger is pointing to this line -- <pre><code>strTmp = strTmp.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces</code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-03-22 19:39:13 <br /> @ Jeppe: Well that's an issue that should actually have been fixed already. Are you sure you're running the latest version? Thanks! <hr /> <strong> <a href="http://jeppeb.nl" rel="nofollow">Jeppe</a> </strong> on 2009-03-16 20:44:39 <br /> strtotime(&quot;last month&quot;), &quot;-1 month&quot;, &quot;next month&quot;, etc. doesn't work. Any ideas? Thanks! <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-02-25 16:58:14 <br /> @ Dave: I've contacted the original author Caio Ariede, and he's been kind enough to fix this function up. All testcases work now! <hr />