» Javascript equivalent for PHP's mktime
134 PHP equivalents
- PHP.JS Licensing
- PHP.JS SVN
- PHP.JS Namespaced
- abs
- addslashes
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff
- array_diff_assoc
- array_diff_key
- array_fill
- array_flip
- array_keys
- array_key_exists
- array_map
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_reverse
- array_search
- array_shift
- array_sum
- array_unique
- array_unshift
- array_values
- base64_decode
- base64_encode
- basename
- bin2hex
- call_user_func_array
- checkdate
- chr
- compact
- count
- count_chars
- crc32
- date
- defined
- dirname
- echo
- empty
- end
- explode
- file
- file_get_contents
- floatval
- function_exists
- get_class
- get_included_files
- htmlentities
- html_entity_decode
- http_build_query
- implode
- include
- include_once
- intval
- in_array
- ip2long
- isset
- is_array
- is_null
- is_numeric
- is_object
- is_string
- join
- levenshtein
- long2ip
- ltrim
- md5
- md5_file
- » mktime
- nl2br
- number_format
- ord
- parse_str
- preg_quote
- printf
- print_r
- rand
- range
- require
- require_once
- reset
- round
- rtrim
- serialize
- setcookie
- sha1
- sha1_file
- shuffle
- sizeof
- soundex
- split
- sprintf
- strcasecmp
- strchr
- strcmp
- stripos
- stripslashes
- strip_tags
- stristr
- strlen
- strnatcmp
- strpbrk
- strpos
- strrev
- strripos
- strrpos
- strstr
- strtolower
- strtoupper
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_split
- substr
- substr_count
- trim
- ucfirst
- ucwords
- unserialize
- urldecode
- urlencode
- utf8_decode
- utf8_encode
- var_export
- wordwrap
PHP to Javascript Project: php.js
This article is part of the 'Porting PHP to Javascript' Project, which aims to decrease the gap between developing for PHP & Javascript.
A lot of people are familiar with PHP's functions, and though Javascript functions are often quite similar, some functions may be missing or addressed differently. The Javascript implementations should be as compliant with the PHP versions as possible, a good indication is that the PHP function manual could also apply to the Javascript version.
Porting crucial PHP functions to Javascript can be fun & useful. Currently some PHP functions have been added, but readers are encouraged to contribute and improve functions by adding comments. Eventually the goal is to save all the functions in one php.js file and make it publicly available for your coding pleasure.
If you choose to contribute, let me know how you want to be credited in the function's comments. You may also want to subscribe to RSS so you receive updates whenever new functions are posted.
This is a Javascript version of the PHP function: mktime.
PHP mktime
Description
mktime - Get Unix timestamp for a date
int mktime([ int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Parameters
-
hour
The number of the hour.
-
minute
The number of the minute.
-
second
The number of seconds past the minute.
-
month
The number of the month.
-
day
The number of the day.
-
year
The number of the year, may be a two or four digit value, with values between 0-69 mapping to 2000-2069 and 70-100 to 1970-2000. On systems where time_t is a 32bit signed integer, as most common today, the valid range for year is somewhere between 1901 and 2038. However, before PHP 5.1.0 this range was limited from 1970 to 2038 on some systems (e.g. Windows).
-
is_dst
This parameter can be set to 1 if the time is during daylight savings time (DST), 0 if it is not, or -1 (the default) if it is unknown whether the time is within daylight savings time or not. If it's unknown, PHP tries to figure it out itself. This can cause unexpected (but not incorrect) results. Some times are invalid if DST is enabled on the system PHP is running on or is_dst is set to 1. If DST is enabled in e.g. 2:00, all times between 2:00 and 3:00 are invalid and mktime() returns an undefined (usually negative) value. Some systems (e.g. Solaris 8) enable DST at midnight so time 0:30 of the day when DST is enabled is evaluated as 23:30 of the previous day.
Note: As of PHP 5.1.0, this parameter became deprecated. As a result, the new timezone handling features should be used instead.
Return Values
mktime() returns the Unix timestamp of the arguments given. If the arguments are invalid, the function returns FALSE (before PHP 5.1 it returned -1).
See Also
Javascript mktime
Source
This is the main source of the Javascript version of PHP's mktime
function mktime() { // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: baris ozdil // + input by: gabriel paderni // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: FGFEmperor // * example 1: mktime( 14, 10, 2, 2, 1, 2008 ); // * returns 1: 1201871402 var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length; d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972); var dateManip = { 0: function(tt){ return d.setHours(tt); }, 1: function(tt){ return d.setMinutes(tt); }, 2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; }, 3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; }, 4: function(tt){ return d.setDate(tt+mb); }, 5: function(tt){ return d.setYear(tt+ma); } }; for( i = 0; i < argc; i++ ){ no = parseInt(argv[i]); if(no && isNaN(no)){ return false; } else if(no){ // arg is number, let's manipulate date object if(!dateManip[i](no)){ // failed return false; } } } return Math.floor(d.getTime()/1000); }
Examples
Currently there is 1 example
Example 1
mktime( 14, 10, 2, 2, 1, 2008 );And that would return
1201871402More about this Project
Download php.js
To easily include it in your code, every function currently available is stored in
Normal
- uncompressed source: php.js (147.1kB)
- minified: php.min.js (54.1kB)
- compressed: php.packed.js (39kB)
Namespaced What is 'namespaced?'
- uncompressed source: php.namespaced.js (181.6kB)
- minified: php.namespaced.min.js (54.5kB)
- compressed: php.namespaced.packed.js (39.4kB)
To download use Right click, Save Link As
Generally the best way is to use a minified version and gzip it
Testing the functions
The number of functions is growing fast and so it becomes hard to maintain quality.
To defeat that danger of bad code, syntax errors, etc, I've added a new feature: php.js tester.
It is an automatically generated page that includes ALL functions in your browser, and then extracts specific testing information from each function's comments. This info is then used to run the function, and the return value is compared to a predefined one.
This way code is always checked on syntax errors, and if it doesn't function correctly anymore after an update, we should also be able to detect it more easily.
If you want, go check it out.
Credits
Respect & awards go to everybody who has contributed in some way so far:
![]() ![]() | Michael White (link) for contributing to: |
| array_count_values, get_included_files, include, include_once, require, require_once, md5, number_format, parse_str, printf, sha1, sprintf, str_pad, strnatcmp, http_build_query, floatval, is_object, print_r | |
![]() ![]() | _argos for contributing to: |
| array_fill, array_pad, array_product, array_rand, compact, count, range, abs, defined, ip2long, long2ip, implode, strcmp, ucwords | |
![]() ![]() | Jonas Raoni Soares Silva (link) for contributing to: |
| shuffle, abs, setcookie, number_format, number_format, soundex, str_repeat, str_replace, str_rot13, ucwords, wordwrap, wordwrap | |
![]() ![]() | Legaev Andrey for contributing to: |
| end, reset, file, file_get_contents, function_exists, include, include_once, http_build_query, is_array, is_object | |
![]() ![]() | Ates Goral (link) for contributing to: |
| array_change_key_case, array_count_values, array_diff_key, get_class, preg_quote, addslashes, count_chars, str_rot13, stripslashes | |
![]() ![]() | Philip Peterson for contributing to: |
| sizeof, round, echo, nl2br, str_replace, strchr, urldecode, urlencode, var_export | |
![]() ![]() | Martijn Wieringa for contributing to: |
| str_ireplace, str_split, strcasecmp, stripos, strnatcmp, substr | |
![]() ![]() | Webtoolkit.info (link) for contributing to: |
| crc32, md5, sha1, utf8_decode, utf8_encode | |
![]() ![]() | Carlos R. L. Rodrigues (link) for contributing to: |
| array_chunk, array_unique, date, levenshtein | |
![]() ![]() | Ash Searle (link) for contributing to: |
| basename, printf, sprintf | |
![]() ![]() | Erkekjetter for contributing to: |
| ltrim, rtrim, trim | |
![]() ![]() | marrtins for contributing to: |
| array_change_key_case, addslashes, stripslashes | |
![]() ![]() | Alfonso Jimenez (link) for contributing to: |
| array_reduce, strpbrk | |
![]() ![]() | Aman Gupta for contributing to: |
| base64_decode, utf8_decode | |
![]() ![]() | Arpad Ray (mailto:arpad@php.net) for contributing to: |
| serialize, unserialize | |
![]() ![]() | Karol Kowalski for contributing to: |
| array_reverse, abs | |
![]() ![]() | Thunder.m for contributing to: |
| base64_decode, base64_encode | |
![]() ![]() | Tyler Akins (link) for contributing to: |
| base64_decode, base64_encode | |
![]() ![]() | mdsjack (link) for contributing to: |
| include, trim | |
![]() ![]() | Alexander Ermolaev (link) for contributing to: |
| trim | |
![]() ![]() | Allan Jensen (link) for contributing to: |
| number_format | |
![]() ![]() | Andrea Giammarchi (link) for contributing to: |
| array_map | |
![]() ![]() | Bayron Guevara for contributing to: |
| base64_encode | |
![]() ![]() | Benjamin Lupton for contributing to: |
| number_format | |
![]() ![]() | Brad Touesnard for contributing to: |
| date | |
![]() ![]() | Brett Zamir for contributing to: |
| str_split | |
![]() ![]() | Cagri Ekin for contributing to: |
| parse_str | |
![]() ![]() | Cord for contributing to: |
| is_array | |
![]() ![]() | David for contributing to: |
| is_numeric | |
![]() ![]() | David James for contributing to: |
| get_class | |
![]() ![]() | DxGx for contributing to: |
| trim | |
![]() ![]() | FGFEmperor for contributing to: |
| mktime | |
![]() ![]() | Felix Geisendoerfer (link) for contributing to: |
| array_key_exists | |
![]() ![]() | FremyCompany for contributing to: |
| isset | |
![]() ![]() | Gabriel Paderni for contributing to: |
| str_replace | |
![]() ![]() | Leslie Hoare for contributing to: |
| rand | |
![]() ![]() | Lincoln Ramsay for contributing to: |
| basename | |
![]() ![]() | MeEtc (link) for contributing to: |
| date | |
![]() ![]() | Mick@el for contributing to: |
| stripslashes | |
![]() ![]() | Nick Callen for contributing to: |
| wordwrap | |
![]() ![]() | Ozh for contributing to: |
| dirname | |
![]() ![]() | Pedro Tainha (link) for contributing to: |
| unserialize | |
![]() ![]() | Peter-Paul Koch (link) for contributing to: |
| date | |
![]() ![]() | Philippe Baumann for contributing to: |
| empty | |
![]() ![]() | Sakimori for contributing to: |
| strlen | |
![]() ![]() | Sanjoy Roy for contributing to: |
| array_diff | |
![]() ![]() | Simon Willison (link) for contributing to: |
| str_replace | |
![]() ![]() | Steve Clay for contributing to: |
| function_exists | |
![]() ![]() | Steve Hilder for contributing to: |
| strcmp | |
![]() ![]() | Steven Levithan (link) for contributing to: |
| trim | |
![]() ![]() | T0bsn for contributing to: |
| crc32 | |
![]() ![]() | Thiago Mata (link) for contributing to: |
| call_user_func_array | |
![]() ![]() | Tim Wiel for contributing to: |
| date | |
![]() ![]() | XoraX (link) for contributing to: |
| dirname | |
![]() ![]() | baris ozdil for contributing to: |
| mktime | |
![]() ![]() | booeyOH for contributing to: |
| preg_quote | |
![]() ![]() | djmix for contributing to: |
| basename | |
![]() ![]() | duncan for contributing to: |
| array_unique | |
![]() ![]() | echo is bad for contributing to: |
| echo | |
![]() ![]() | gabriel paderni for contributing to: |
| mktime | |
![]() ![]() | ger for contributing to: |
| html_entity_decode | |
![]() ![]() | john (link) for contributing to: |
| html_entity_decode | |
![]() ![]() | kenneth for contributing to: |
| explode | |
![]() ![]() | penutbutterjelly for contributing to: |
| str_ireplace | |
![]() ![]() | stensi for contributing to: |
| intval | |
Your name here?
Contributing is as easy as adding a comment with better code, or code for a new function.
Any contribution leading to improvement will directly get your name & link here.
Coming Project features
Project features that we are currently working on:
- Versioning. Individual functions are versioned, but the entire library should be versioned as well.
- Light. A lightweight version of php.js should be made available with only common functions in it.
Like this article?
|
Then Dzone it! Or use another bookmark button below to show your support & help me spread the word. |
Hot StuffFlaming articles» Survive heavy traffic with you... | RelatedArticles like this one» PHP.JS Licensing |
tags: programming, php, javascript, phpjs
category: Programming - Javascript - PHP equivalents
read: 4,528 times










tagcloud
#20. Kevin on 22 April 2008
@ Philip Peterson: If it's even possible to set the timezone for an entire page... I think you can only do this to a date object.
So maybe the right approach is to include a
In the test to neutralize the end user's timezone?#19. Kevin on 22 April 2008
#18. Philip Peterson on 21 April 2008
#17. Kevin on 20 April 2008
#16. Philip Peterson on 20 April 2008
#15. Kevin on 19 April 2008
#14. FGFEmperor on 18 April 2008
OK, now setting a month to something greater than 11 would not increase the year...
My correction (also for hours, minutes and seconds...
Basically I added 'ma' and 'mb' to the vars, and summed that on Years and Days... =)#13. Kevin on 02 April 2008
#12. FGFEmperor on 31 March 2008
change
d.setYear(1970);
to
d.setYear(1972);
... [more] Why? Because this way Leap Years are gonna work.
#11. FGFEmperor on 31 March 2008
Got It!
Add
after The catch here is that d = New Date(); sets d to the current date. What was happening is that today is the 31th, and I the function was setting the month to February, but the day was still the 31th, then it would set the month to March (since February has only 28/29 days). ;-)#10. FGFEmperor on 31 March 2008
alert('PHP: <?= mktime(0,0,0,4,5,2009); ?>\nJS: '+mktime(0,0,0,4,5,2009));
returns this:
PHP: 1238900400
JS: 1241550694
... [more] Any idea why? That way, I should get 4/5/2009 for both, but I'm getting 5/5/2009 on the JS version...
#9. Kevin on 23 March 2008
http://kevin.vanzonneveld.net/test.php
But I've added the parseInt function. Does that solve the bug? Thanks gabriel!
#8. gabriel paderni on 22 March 2008
If i'm not mistaken there's a bug when using strings as parameters:
here is the test I've made:
Strangely in the test this shows up only for August and September. The solution may be to remove the leading zero when the parameter is a string. (Tested with: Firefox and Internet explorer, same result.)#7. Kevin on 02 March 2008
http://kevin.vanzonneveld.net/test.php
#6. Michael White on 02 March 2008
#5. Kevin on 21 February 2008
#4. baris ozdil on 21 February 2008
#3. Kevin on 03 February 2008
Other than that: Nice work again man, you've earned yourself 2 gold medals :)
#2. _argos on 03 February 2008
function array_product ( input ) {
var Index = 0, Product = 1;
... [more] if ( input instanceof Array ) {
while ( Index < input.length ) {
Product *= ( !isNaN ( input [ Index ] ) ? input [ Index ] : 0 );
Index++;
}
} else {
Product = null;
}
return Product;
}
#1. _argos on 03 February 2008