Tiny script to pass hour to string

I’m very much a beginner and am interested in learning simple
things which I can later apply for practical purposes. Example,
I’d like to pass the time as 2 digit hour to a string, meaning
I’d like what’s returned by strftime(’%H’); passed to my string.

<?php
$moda = strftime('%m%d');
$oldhr = strftime('%H');
$mnsc = strftime('%M%S');
echo "moda=$moda\n";
echo "oldhr=$oldhr\n";
echo "mnsc=$mnsc\n";
echo "newhr must also be printed\n";
echo "newhr must be 4 hrs earlier";
?>

Any useful suggestion for a beginner in php will be appreciated!
I’m running my scripts exclusively from windows cli for now.
With the helpful replies of 2 members I managed to resolve my
original issue ‘and more.’ Final objective is to make time 4 hrs
earlier, which I’ll work toward soon. Again, thank you members.

You can post code on the forum by either putting bbcode [code][/code] tags or three markdown back-ticks ```, before and after your code. The opening and closing bbcode/markdown must be on their own lines, with nothing else before/after them on the line. I have edited your first post.

The posted code does assign the return value from that function call to that variable. What makes you think it doesn’t?

BTW: strftime() -

This function has been DEPRECATED as of PHP 8.1.0. Relying on this function is highly discouraged.

Noted, thanks for editing my post. I’ll look into DateTime() and date() as well. Ideally I’ll even find something to help me adjust the hour, which is my ultimate objective with this script.

<?php
// Get the current time
$currentTime = time();

// Get the time for 4 hours ago
$timeFourHoursAgo = $currentTime - (4 * 60 * 60);

$moda = date('md', $currentTime);
$oldhr = date('H', $currentTime);
$mnsc = date('is', $currentTime);

$newhr = date('H', $timeFourHoursAgo);

echo "moda=$moda\n";
echo "oldhr=$oldhr\n";
echo "mnsc=$mnsc\n";
echo "newhr=$newhr\n";  // newhr is 4 hours earlier
?>

This code will output the current month and day, the current hour, the new hour (which is 4 hours earlier than the current hour), and the current minute and second.

Please note that if the current time is less than 4 hours past midnight (00:00), the new hour will be on the previous day. For example, if the current time is 01:00, the new hour will be 21:00 on the previous day.
Good Luck

Sponsor our Newsletter | Privacy Policy | Terms of Service