Skip to content

Commit 72d9fee

Browse files
authored
Merge pull request #246 from Kharhamel/dateFunction
Date function
2 parents 7026ace + f531da1 commit 72d9fee

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

generated/datetime.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,69 @@ function date_sunset(int $timestamp, int $returnFormat = SUNFUNCS_RET_STRING, fl
357357
}
358358

359359

360+
/**
361+
* Returns a string formatted according to the given format string using the
362+
* given integer timestamp or the current time
363+
* if no timestamp is given. In other words, timestamp
364+
* is optional and defaults to the value of time.
365+
*
366+
* @param string $format Format accepted by DateTimeInterface::format.
367+
* @param int $timestamp The optional timestamp parameter is an
368+
* integer Unix timestamp that defaults to the current
369+
* local time if a timestamp is not given. In other
370+
* words, it defaults to the value of time.
371+
* @return string Returns a formatted date string. If a non-numeric value is used for
372+
* timestamp, FALSE is returned and an
373+
* E_WARNING level error is emitted.
374+
* @throws DatetimeException
375+
*
376+
*/
377+
function date(string $format, int $timestamp = null): string
378+
{
379+
error_clear_last();
380+
if ($timestamp !== null) {
381+
$result = \date($format, $timestamp);
382+
} else {
383+
$result = \date($format);
384+
}
385+
if ($result === false) {
386+
throw DatetimeException::createFromPhpError();
387+
}
388+
return $result;
389+
}
390+
391+
392+
/**
393+
* Identical to the date function except that
394+
* the time returned is Greenwich Mean Time (GMT).
395+
*
396+
* @param string $format The format of the outputted date string. See the formatting
397+
* options for the date function.
398+
* @param int $timestamp The optional timestamp parameter is an
399+
* integer Unix timestamp that defaults to the current
400+
* local time if a timestamp is not given. In other
401+
* words, it defaults to the value of time.
402+
* @return string Returns a formatted date string. If a non-numeric value is used for
403+
* timestamp, FALSE is returned and an
404+
* E_WARNING level error is emitted.
405+
* @throws DatetimeException
406+
*
407+
*/
408+
function gmdate(string $format, int $timestamp = null): string
409+
{
410+
error_clear_last();
411+
if ($timestamp !== null) {
412+
$result = \gmdate($format, $timestamp);
413+
} else {
414+
$result = \gmdate($format);
415+
}
416+
if ($result === false) {
417+
throw DatetimeException::createFromPhpError();
418+
}
419+
return $result;
420+
}
421+
422+
360423
/**
361424
* Returns the Unix timestamp corresponding to the arguments
362425
* given. This timestamp is a long integer containing the number of

generated/functionsList.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
'curl_share_errno',
6868
'curl_share_setopt',
6969
'curl_unescape',
70+
'date',
7071
'date_parse',
7172
'date_parse_from_format',
7273
'date_sunrise',
@@ -191,6 +192,7 @@
191192
'getprotobynumber',
192193
'get_headers',
193194
'glob',
195+
'gmdate',
194196
'gmp_binomial',
195197
'gmp_export',
196198
'gmp_import',

generator/src/DocPage.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public function detectFalsyFunction(): bool
112112
if (preg_match('/&gd\.return\.identifier;/m', $file)) {
113113
return true;
114114
}
115+
//used for date
116+
if (preg_match('/If a non-numeric value is used for
117+
\<parameter\>timestamp\<\/parameter\>, &false; is returned/m', $file)) {
118+
return true;
119+
}
115120

116121
//used to detect imagecreatefromstring
117122
if (preg_match('/If the arguments are invalid, the function returns &false;/m', $file)) {

generator/tests/DocPageTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function testDetectFalsyFunction()
1818
$fsockopen = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/network/functions/fsockopen.xml');
1919
$arrayReplace = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/array/functions/array-replace.xml');
2020
$mktime = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/datetime/functions/mktime.xml');
21+
$date = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/datetime/functions/date.xml');
2122

2223
$this->assertTrue($pregMatch->detectFalsyFunction());
2324
$this->assertFalse($implode->detectFalsyFunction());
@@ -29,6 +30,7 @@ public function testDetectFalsyFunction()
2930
$this->assertTrue($fsockopen->detectFalsyFunction());
3031
$this->assertFalse($arrayReplace->detectFalsyFunction());
3132
$this->assertTrue($mktime->detectFalsyFunction());
33+
$this->assertTrue($date->detectFalsyFunction());
3234
}
3335

3436
public function testDetectNullsyFunction()

rector-migrate-0.7.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
'curl_share_errno' => 'Safe\curl_share_errno',
7878
'curl_share_setopt' => 'Safe\curl_share_setopt',
7979
'curl_unescape' => 'Safe\curl_unescape',
80+
'date' => 'Safe\date',
8081
'date_parse' => 'Safe\date_parse',
8182
'date_parse_from_format' => 'Safe\date_parse_from_format',
8283
'date_sunrise' => 'Safe\date_sunrise',
@@ -201,6 +202,7 @@
201202
'getprotobynumber' => 'Safe\getprotobynumber',
202203
'get_headers' => 'Safe\get_headers',
203204
'glob' => 'Safe\glob',
205+
'gmdate' => 'Safe\gmdate',
204206
'gmp_binomial' => 'Safe\gmp_binomial',
205207
'gmp_export' => 'Safe\gmp_export',
206208
'gmp_import' => 'Safe\gmp_import',

0 commit comments

Comments
 (0)