PHP: Get last digit of year (example: 2015 == 5)

2019-02-28 06:09发布

问题:

How may I convert my function to show the last and/or the second last digit in the "Y" tag?

Here is my code:

<?php
$timestamp = time();     //Timestamp from server
$user_time = 1597429549; //Timestamp from database

$time = ($user_time-$timestamp); // Should be around 4 years

echo gmdate("Y m d H i s", $time); //This will output 1974 10 20 19 59 34
?>

How can I convert this to display time as:

4 10 20 19 59 34 or 0004 10 20 19 59 34

Instead of:

1974 10 20 19 59 34

回答1:

Use modulus operator:

2015 % 10 = 5


回答2:

Substr did the trick, thank you guys for pointing me in the right direction.

    <?php
$timestamp = time();     //Timestamp from server
$user_time = 1597429549; //Timestamp from database

$time = ($user_time-$timestamp); // Should be around 4 years

// This did the trick:

$gmdate = gmdate("y m d H i s", $time);
$rest = substr("$gmdate", -16);
?>