6
  1. i wanna to know how strtotime (php Date function) work?
  2. how Parse string Like "15 September 2012" to timeStamp
  3. is there any better algorithm?

my purpose is changing this function for Persian Language

  • 1
    Here is http://stackoverflow.com/a/1268380 a Java implementation, but do not know if it is taken directly from the source code of PHP strtotime function. – Lobo Apr 12 '12 at 08:06

5 Answers5

10

You can browse the source code of PHP ( https://github.com/php/php-src) and search function to see its implementation.

UPDATE

Here is the algorithm of the function strtotime () https://github.com/php/php-src/blob/master/ext/date/php_date.c#L1324

Regards!.

Lobo
  • 4,001
  • 8
  • 37
  • 67
  • 9
    Instead of linking to master (which will change over time, and break your link) here is a link to the 5.3.23 tag instead. This won't change. https://github.com/php/php-src/blob/php-5.3.23/ext/date/php_date.c#L1397 – Mark E. Haase Apr 03 '13 at 14:45
4

The method that actually parses the string is in parse_date.re's scan() method. There you will find all strtotime keywords and their behaviors.

That library is included from https://github.com/derickr/timelib.

Tim Trinidad
  • 137
  • 1
  • 1
  • 6
3

since strtotime accepts English input, I'd recommend taking the Persian input: "15 (SOMETHING_PERSIAN) 2012" and replace the required string (you need some RegExp and a switch statement, I guess) and make it "15 (SOMETHING_ENGLISH) 2012" and THEN send it to strtotime

Adi
  • 5,089
  • 6
  • 33
  • 47
3

i wanna to know how strtotime (php Date function) work?

Go here: https://github.com/php/php-src/blob/master/ext/date/lib/parse_date.c

Search for timelib_strtotime.

jon
  • 5,986
  • 5
  • 28
  • 35
  • The source file is 25,000 lines long, clearly generated by a yacc/bison-like tool. Although it may be adequate as a working implementation, it is not useful as any sort of resource for learning how to it is actually done. – markt1964 Nov 07 '17 at 00:05
  • I've tried to look into that source code, and I felt like abyss is staring back at me. – baldrs Mar 12 '18 at 09:23
  • One probably rather wants to look at https://github.com/php/php-src/blob/2bf451b92594a70fe745b9d27783ffe211d7940e/ext/date/lib/parse_date.re - that's the file used to generate the C code. – Zepporle Nov 17 '21 at 21:44
2

forget about it, use php Intl extension. in future will be a part of php core.

<?php
    // format
    $format = datefmt_create('fa_IR@calendar=persian', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL, 'yyyy/MM/dd HH:mm');

    // time in locale as you wish
    var_dump(datefmt_format($format, time())); // '۱۳۹۱/۰۲/۰۴ ۱۹:۱۵'

support ICU project if you want to support Persian in i18n for all languages.

php intl extension

icu-project.org