Core\Time\DateTime::second
$dateTime->second(): uintReturns the second of a date and time, as a uint from 0 to 59.
The second is read in the time zone of the value. It is a whole number. The part of the second after it is returned by nanoseconds.
The result has the same name and type as the second option of with. So $d->with({ second: 0, nanoseconds: 0 }) gives the start of the same minute. The second option must be from 0 to 59, and a value outside that range throws a RuntimeError.
Good to know: a date and time made from a TimeOfDay without a second option has second 0.
Return value
Returns uint. The second, from 0 to 59.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\DateTime::year, Core\Time\DateTime::month, Core\Time\DateTime::day, Core\Time\DateTime::hour, Core\Time\DateTime::minute, Core\Time\DateTime::second and Core\Time\DateTime::nanoseconds return the parts of a date and time as numbers. Each one reads the clock in the zone of the value. Core\Time\DateTime::year returns an int, and the others return a uint. |
Examples
Read the second
<?nvs// Shows how to read the second of a date and time as a number.
var $login = Core\Time\DateTime::parse('2024-10-02 18:04:37', 'yyyy-MM-dd HH:mm:ss', 'Europe/Lisbon');
// `second` returns a `uint` from 0 to 59. This prints 37.echo $login->second(), "\n";
// A time without seconds has second 0. This prints 0.echo Core\Time\DateTime::fromIso('2024-10-02 18:04', 'Europe/Lisbon')->second(), "\n";370Group log lines by minute
<?nvs// Shows how to set the seconds to 0, so that log lines from the same minute are counted together.
array<string> $lines = [ '2024-05-14T09:12:03Z', '2024-05-14T09:12:41Z', '2024-05-14T09:13:00Z', '2024-05-14T09:13:59Z', '2024-05-14T09:13:59Z',];
// `with` sets the second and the nanoseconds to 0. The result is the start of the minute.// The lines are in time order, so a new minute starts a new group.string $current = '';int $count = 0;foreach ($lines as string $text) { var $time = Core\Time\Instant::fromIso($text)->in('UTC'); string $minute = $time->with({ second: 0, nanoseconds: 0 })->format('HH:mm'); if ($minute != $current && $count > 0) { echo $current, ': ', $count, " lines\n"; $count = 0; } $current = $minute; $count = $count + 1;}
// This prints 09:12: 2 lines and then 09:13: 3 lines.echo $current, ': ', $count, " lines\n";09:12: 2 lines09:13: 3 linesSeconds since midnight
<?nvs// Shows how to count the seconds since midnight, for a daily timer in a factory.
class Clock{ public static function secondsSinceMidnight(Core\Time\DateTime $time): uint { return $time->hour() * 3600 + $time->minute() * 60 + $time->second(); }}
var $zone = Core\Time\Zone::of('Asia/Seoul');
// One minute after midnight is 60 seconds. This prints 60.echo Clock::secondsSinceMidnight(Core\Time\DateTime::fromIso('2024-01-15 00:01', $zone)), "\n";
// 08:30:15 is 8 hours, 30 minutes and 15 seconds after midnight. This prints 30615.echo Clock::secondsSinceMidnight(Core\Time\DateTime::fromIso('2024-01-15 08:30:15', $zone)), "\n";6030615