Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] [PATCH] new function for real local date

Posted by Anonymous User 
Anonymous User
[PHP-DEV] [PATCH] new function for real local date
March 13, 2018 08:30PM
As there is no function for getting a real local date for day names and month names, I wrote a function for that. Maybe an option for a new PHP version?

<?php

function localdate(string $format, int $timestamp, string $locale) {

$oldlocale = setlocale(LC_TIME, 0);
setlocale(LC_TIME, $locale);

$date = '';
for ($i = 0; $i < strlen($format); $i++) {

$char = substr($format, $i, 1);
switch ($char) {
case 'D':
$date .= strftime('%a', $timestamp);
break;
case 'l':
$date .= strftime('%A', $timestamp);
break;
case 'M':
$date .= strftime('%b', $timestamp);
break;
case 'F':
$date .= strftime('%B', $timestamp);
break;
default:
$date .= date($char, $timestamp);
break;
}
}

setlocale(LC_TIME, $oldlocale);

return $date;
}

?>


with kind regards,
Ruud Bakker


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Andreas Heigl
Re: [PHP-DEV] [PATCH] new function for real local date
March 13, 2018 08:50PM
Hey Ruud.


Am 13.03.18 um 20:18 schrieb ruud.bakker@caiway.nl:
> As there is no function for getting a real local date for day names and month names, I wrote a function for that. Maybe an option for a new PHP version?
>
> <?php
>
> function localdate(string $format, int $timestamp, string $locale) {
>
> $oldlocale = setlocale(LC_TIME, 0);
> setlocale(LC_TIME, $locale);
>
> $date = '';
> for ($i = 0; $i < strlen($format); $i++) {
>
> $char = substr($format, $i, 1);
> switch ($char) {
> case 'D':
> $date .= strftime('%a', $timestamp);
> break;
> case 'l':
> $date .= strftime('%A', $timestamp);
> break;
> case 'M':
> $date .= strftime('%b', $timestamp);
> break;
> case 'F':
> $date .= strftime('%B', $timestamp);
> break;
> default:
> $date .= date($char, $timestamp);
> break;
> }
> }
>
> setlocale(LC_TIME, $oldlocale);
>
> return $date;
> }

May I ask what you actually mean by "real local date for day names and
month names"?

Or is this something like this:

<?php
$fmt = IntlDateFormatter::formatObject(new DateTime(), 'MMMM', 'nl_NL');
echo 'Month is ' . $fmt;
// will output "Month is maart" for the next few days ;)
?>

Cheers

Andreas

--
,,,
(o o)
+---------------------------------------------------------ooO-(_)-Ooo-+
| Andreas Heigl |
| mailto:[email protected] N 50°22'59.5" E 08°23'58" |
| http://andreas.heigl.org http://hei.gl/wiFKy7 |
+---------------------------------------------------------------------+
| http://hei.gl/root-ca |
+---------------------------------------------------------------------+
Sorry, only registered users may post in this forum.

Click here to login