Skip to main content
Timeformat/strftime with php 8 Started by Ruth · · Read 8637 times 0 Members and 1 Guest are viewing this topic. previous topic - next topic

Timeformat/strftime with php 8

Hi!

This string about time format in the help file on line 111 is displayed in the ACP but also for members in their profiles:
Code: [Select]
$helptxt['time_format'] = '<strong>Time Format</strong><br />
You have the power to adjust how the time and date look for yourself. There are a lot of little letters, but it\'s quite simple.
The conventions follow PHP\'s strftime function and are described as below (more details can be found at <a href="http://www.php.net/manual/function.strftime.php" target="_blank" class="new_win">php.net</a>).<br />
<br />
The following characters are recognized in the format string:<br />
<span class="smalltext">
&nbsp;&nbsp;%a - abbreviated weekday name<br />
&nbsp;&nbsp;%A - full weekday name<br />
&nbsp;&nbsp;%b - abbreviated month name<br />
&nbsp;&nbsp;%B - full month name<br />
&nbsp;&nbsp;%d - day of the month (01 to 31) <br />
&nbsp;&nbsp;%D<strong>*</strong> - same as %m/%d/%y <br />
&nbsp;&nbsp;%e<strong>*</strong> - day of the month (1 to 31) <br />
&nbsp;&nbsp;%H - hour using a 24-hour clock (range 00 to 23) <br />
&nbsp;&nbsp;%I - hour using a 12-hour clock (range 01 to 12) <br />
&nbsp;&nbsp;%m - month as a number (01 to 12) <br />
&nbsp;&nbsp;%M - minute as a number <br />
&nbsp;&nbsp;%p - either &quot;am&quot; or &quot;pm&quot; according to the given time<br />
&nbsp;&nbsp;%R<strong>*</strong> - time in 24 hour notation <br />
&nbsp;&nbsp;%S - second as a decimal number <br />
&nbsp;&nbsp;%T<strong>*</strong> - current time, equal to %H:%M:%S <br />
&nbsp;&nbsp;%y - 2 digit year (00 to 99) <br />
&nbsp;&nbsp;%Y - 4 digit year<br />
&nbsp;&nbsp;%% - a literal \'%\' character <br />
<br />
<em>* Does not work on Windows-based servers.</em></span>';

I wanted to add a few words there, because members will mostly use the scroll down menu, if they want to select another time format. But they also could use the field beyond and insert another time fomat there:

time_1.jpg time_2.jpg




I was on https://www.php.net/manual/en/function.strftime.php and saw a lot of "DEPRECATED" there.

I did not understand proper, what is said there. Do I need to change time format in future with php 8?

Re: Timeformat/strftime with php 8

Reply #1

Quote from: Ruth – I did not understand proper, what is said there. Do I need to change time format in future with php 8?

No, they have deprecated that function in favour of others. I put in a fix for here which should keep most of the old ways on > PHP 8 however it does need a lot of testing especially with languages other than English.

Re: Timeformat/strftime with php 8

Reply #2

Thank you, Tino!  :smiley:  This would be great.

I think, most german forums are using this time format by default:
Code: [Select]
%d. %B %Y, %H:%M %p
09. April 2023, 12:47 Uhr

The p at the end allows us an "Uhr" (clock) which we have allways instead of am/pm.
And the english/american should have another time format on german forums, which is the most common for them.
But i think, this much of possibillities, like it is now,  is really not needed.

Re: Timeformat/strftime with php 8

Reply #3

I think, there is a problem with this custom php block, which might be caused by php 8...

Spuds and Emanuele were writing this code once for our forum.
I use it in 3 variants on forum and in portal.

Code: [Select]
global $context, $settings, $scripturl, $txt, $user_info, $modSettings, $posts, $color_profile;

$db = database();

$exclude_boards = null;

// Number of items to show
$num_recent = 25;

// Show the body preview on hover
$show_preview = true;

// Show the body preview text in the block
$show_body = false;

// Number of characters to show in the preview, 0 is all
$preview_char = 128;

// First or last message for the preview
$preview_last = false;

if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
$exclude_boards = array($modSettings['recycle_board']);
else
$exclude_boards = empty($exclude_boards) ? array() : $exclude_boards;

$stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'moved', 'recycled', 'wireless');
$icon_sources = array();
foreach ($stable_icons as $icon)
$icon_sources[$icon] = 'images_url';

// If empty, no previews at all
if (empty($show_preview) && empty($show_body))
$preview_bodies = '';
// If 0 means everything
elseif (empty($preview_char))
$preview_bodies = ', ml.body AS last_body, mf.body AS first_body';
// else a SUBSTRING
else
$preview_bodies = ', SUBSTRING(ml.body, 1, ' . ($preview_char + 256) . ') AS last_body, SUBSTRING(mf.body, 1, ' . ($preview_char + 256) . ') AS first_body';

// Find all the posts in distinct topics.  Newer ones will have higher IDs.
$request = $db->query('substring', '
SELECT
mf.subject,
ml.id_msg, ml.id_topic, ml.id_member, ml.poster_time,
b.id_board, b.name AS bName,
t.num_replies, t.num_views, t.id_first_msg, t.id_last_msg,
IFNULL(mem.real_name, ml.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= ml.id_msg_modified AS is_read,
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . ', mf.smileys_enabled, mf.icon, mg.online_color' .
$preview_bodies . '
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
LEFT JOIN {db_prefix}boards AS b ON (b.id_board = mf.id_board)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ml.id_member)' . (!$user_info['is_guest'] ? '
LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})' : '') . '
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
WHERE t.id_last_msg >= {int:likely_max_msg}
AND b.id_board = t.id_board' . (empty($exclude_boards) ? '' : '
AND b.id_board NOT IN ({array_int:exclude_boards})') . '
AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
AND t.approved = {int:is_approved}
AND ml.approved = {int:is_approved}' : '') . '
ORDER BY t.id_last_msg DESC
LIMIT {int:limit}',
array(
'current_member' => $user_info['id'],
'exclude_boards' => $exclude_boards,
'limit' => (int) $num_recent,
'is_approved' => 1,
'likely_max_msg' => $modSettings['maxMsgID'] - 35 * min($num_recent, 5),
'preview_char' => $preview_char,
)
);
$posts = array();
$color_ids = array();
while ($row = $db->fetch_assoc($request))
{
// Shorten and censor the body as needed
$row['body'] = '';
if (!(empty($show_preview) && empty($show_body)))
{
if (!empty($preview_char))
{
$row['body'] = strip_tags(strtr(parse_bbc($preview_last ? $row['last_body'] : $row['first_body'], false, $row['id_first_msg']), array('<br />' => "\n", ' ' => ' ')));
$row['body'] = Util::shorten_text($row['body'], !empty($modSettings['preview_characters']) ? $modSettings['preview_characters'] : $preview_char, true);
}
elseif (!empty($show_preview))
$row['body'] = strip_tags(strtr(parse_bbc($preview_last ? $row['last_body'] : $row['first_body'], false, $row['id_first_msg']), array('<br />' => "\n", ' ' => ' ')));

censorText($row['body']);
}

// Censor the subject.
$row['subject'] = preg_replace('/^' . preg_quote($txt['response_prefix']) . '/', '', $row['subject']);
censorText($row['subject']);

// Collect the color ids :)
$color_ids[$row['id_member']] = $row['id_member'];

if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';

// Build the array.
$posts[] = array(
'board' => array(
'id' => $row['id_board'],
'name' => $row['bName'],
'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bName'] . '</a>'
),
'topic' => $row['id_topic'],
'poster' => array(
'id' => $row['id_member'],
'name' => $row['poster_name'],
'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
),
'subject' => $row['subject'],
'short_subject' => Util::shorten_text($row['subject'], 25),
'preview' => $row['body'],
'time' => standardTime($row['poster_time']),
'timestamp' => forum_time(true, $row['poster_time']),
'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new">' . $row['subject'] . '</a>',
'new' => !empty($row['is_read']),
'new_from' => $row['new_from'],
'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" style="vertical-align: middle;" alt="' . $row['icon'] . '" />',
'views' => $row['num_views'],
'replies' => $row['num_replies'],
);
}
$db->free_result($request);

// Colorization
if (!empty($color_ids) && sp_loadColors($color_ids) !== false)
{
foreach ($posts as $key => $value)
{
if (!empty($color_profile[$value['poster']['id']]['link']))
$posts[$key]['poster']['link'] = $color_profile[$value['poster']['id']]['link'];
}
}

$context['recent_topics'] = $posts;

// Just return if we have no results
if (empty($posts))
return $txt['error_sp_no_posts_found'];

// Preview tips?
if (!empty($show_preview))
addInlineJavascript('
$(document).ready(function () {
$(".sp_custom_tr2").SiteTooltip({
hoverIntent: {
sensitivity: 10,
interval: 150,
timeout: 50
}
});
});', true);

echo '
<style>
.sp_custom_tr {display: table-cell; padding: 0 8px;border-bottom:1px dashed #6394BD;text-align: center;}
.sp_custom_tr1 {display: table-cell;border-bottom:1px dashed #6394BD;width:100%;}
.sp_custom_tr2 {font-weight: bold;}

</style>

<div>';

foreach ($posts as $post)
{
echo '
<div style="display: table-row">
<div class="sp_custom_tr centericon">
', $post['icon'], '
</div>
<div class="sp_custom_tr1" >
', $post['new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow"><span class="new_posts"> ' . $txt['new'] . '</span></a>', '
<a class="sp_custom_tr2" href="', $post['href'], '" title="', $post['preview'], '">', $post['subject'], '</a>';

echo '
<div class="smalltext">
', $txt['by'], ' ', $post['poster']['link'], ' ', $txt['in'], ' ', $post['board']['link'], '
</br />
', $post['time'], ' | ', $txt['views'], ': ', $post['views'], ' | ', $txt['replies'], ': ', $post['replies'], '
</div>';

if (!empty($show_body))
echo '
<div class="middletext">
', $post['preview'], '
</div>';

echo '
</div>
</div>';
}

echo '
</div>';

The small one on forum is still working proper. It displayes the time format this way and correct:

Code: [Select]
<div class="sp_custom_tr1" >
', $post['new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow"><span class="new_posts"> ' . $txt['new'] . '</span></a>', '
<a class="sp_custom_tr2" href="', $post['href'], '" title="', $post['preview'], '">', $post['subject'], '</a>';

echo '
<div class="smalltext">
', $txt['by'], ' ', $post['poster']['link'], ' ', $txt['in'], ' ', $post['board']['link'], ' - ', $post['time'], '
</div>';


Aktuelle Themen Forum.jpg

But the "big one" in portal shows "Uhr" twice together with "today" and "yesterday". And it does tell "Zeitformat" ("time format") instead of the time. I don't understand why. Should it not be just the same like in the other portal block?

Aktuelle Themen Portal.jpg

Code: [Select]
<div class="sp_custom_tr1" >
', $post['new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow"><span class="new_posts"> ' . $txt['new'] . '</span></a>', '
<a class="sp_custom_tr2" href="', $post['href'], '" title="', $post['preview'], '">', $post['subject'], '</a>';

echo '
<div class="smalltext">
', $txt['by'], ' ', $post['poster']['link'], ' ', $txt['in'], ' ', $post['board']['link'], '
</br />
', $post['time'], ' | ', $txt['views'], ': ', $post['views'], ' | ', $txt['replies'], ': ', $post['replies'], '
</div>';

On the smart phone of my friend this portal block shows the time correct....but not on my laptop.

Re: Timeformat/strftime with php 8

Reply #4

It could be different settings in Enable shorthand date display   That setting is found in configuration - features and options - layout.  Are those different between the two forums and what are they set as? 

Re: Timeformat/strftime with php 8

Reply #5

I don't find such a setting there, Spuds?

All I have in ACP is this:
Code: [Select]
%d. %B %Y, %H:%M %p
And i have enabled "show today and yesterday" in the settings.

Both is the same in both forums.

The two screenshots are both from elk11, the upgraded forum with the newest portal version.
The "small block" on forum displays eveything correct.
The "big block" on portal does not.



Edit:

I just created a new custom block on elk11 with this php code in it. In the preview all is displayed correct.
But it is displayed wrong then in portal....but not on forum. This is really weird.
Last Edit: May 27, 2023, 10:48:55 am by Ruth

Re: Timeformat/strftime with php 8

Reply #6

I made a few more tests and created new blocks. Some only for portal and another one, which is displayed on forum and on portal.

This block "recent topics", which comes with SimplePortal, displays everything correct in portal.

But the custom php block is only displayed correct on forum. 
On portal each variant of this php-code will have "Uhr" twice and "Timeformat" instead of the time.

Re: Timeformat/strftime with php 8

Reply #7

I'm still chasing this one:person_running:

I did find one error which may help, or not, but it is a bug.

in Load.php find the following:
Code: [Select]
	// Allow overriding the board wide time/number formats.
if (empty($user_settings['time_format']) && !empty($txt['time_format']))
$user_info['time_format'] = $txt['time_format'];
replace it with
Code: [Select]
	// Allow overriding the board wide time/number formats.
if (empty($user_settings['time_format']) && !empty($modSettings['time_format']))
$user_info['time_format'] = $modSettings['time_format'];

The system is supposed to use the time format set in the admin panel, unless the user has set their own.  The error above would use the actual text string of Time Format instead of the system default formatting, in rare instances.

The other thing to check is if in your profile you set a custom time format

Re: Timeformat/strftime with php 8

Reply #8

I changed it in Load.php, Spuds.

This custom block is working now perfect again!  :smiley:  Thank you very much!

It is the most important block to us...I am very happy now.

 

Re: Timeformat/strftime with php 8

Reply #9

Awesome !!!  :grin: