Broad Network


Datetime and Seconds in PHP

Date and Time in PHP with Security Considerations - Part 3

Foreword: In this part of the series I talk about the relationship between datetime and seconds in PHP.

By: Chrysanthus Date Published: 19 Jan 2019

Introduction

This is part 3 of my series, Date and Time in PHP. In this part of the series I talk about the relationship between datetime and seconds in PHP. You should have read the previous parts of series before reaching here, as this is the continuation.

The time() Function
This function returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). The syntax is:

    int time ( void )

Try the following code:

<?php

    $noSecs = time();

    echo $noSecs;

?>

I tried it and I had:

    1547128443

Seconds to Local Time
You can convert any number of seconds (not necessarily current time) to local time. The syntax is:

    string date ( string $format [, int $timestamp = time() ] )

If the second argument is not given, it is taken as the current time. Try the following code:

<?php

    $dt = date('m-d-Y H:i:s', 2000000000);

    echo $dt;

?>

Note that there are no commas to separate the digits in the number of seconds. I tried it and had:

    05-18-2033 03:33:20

Seconds to GMT
You can convert any number of seconds (not necessarilly current time) to GMT. The syntax is:

    string gmdate ( string $format [, int $timestamp = time() ] )

If the second argument is not given, it is taken as the current time. Try the following code:

<?php

    $dt = gmdate('m-d-Y H:i:s', 2000000000);

    echo $dt;

?>

Note that there are no commas to separate the digits in the number of seconds. I tried it and had:

    05-18-2033 03:33:20

Datetime to Seconds
You can convert datetime (not necessarilly current datetime) to seconds. The syntax isL

int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

The function returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.

Parameters:

hour
    The number of the hour relative to the start of the day determined by month, day and year. Negative values reference the hour before midnight of the day in question. Values greater than 23 reference the appropriate hour in the following day(s).

minute
    The number of the minute relative to the start of the hour. Negative values reference the minute in the previous hour. Values greater than 59 reference the appropriate minute in the following hour(s).

second
    The number of seconds relative to the start of the minute. Negative values reference the second in the previous minute. Values greater than 59 reference the appropriate second in the following minute(s).

month
    The number of the month relative to the end of the previous year. Values 1 to 12 reference the normal calendar months of the year in question. Values less than 1 (including negative values) reference the months in the previous year in reverse order, so 0 is December, -1 is November, etc. Values greater than 12 reference the appropriate month in the following year(s).

day
    The number of the day relative to the end of the previous month. Values 1 to 28, 29, 30 or 31 (depending upon the month) reference the normal days in the relevant month. Values less than 1 (including negative values) reference the days in the previous month, so 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s).

year
    The number of the year, may be a two or four digit value, with values between 0-69 mapping to 2000-2069 and 70-100 to 1970-2000. On systems where time_t is a 32bit signed integer, as most common today, the valid range for year is somewhere between 1901 and 2038. However, before PHP 5.1.0 this range was limited from 1970 to 2038 on some systems (e.g. Windows).

is_dst
    This parameter can be set to 1 if the time is during daylight savings time (DST), 0 if it is not, or -1 (the default) if it is unknown whether the time is within daylight savings time or not. If it's unknown, PHP tries to figure it out itself. This can cause unexpected (but not incorrect) results. Some times are invalid if DST is enabled on the system PHP is running on or is_dst is set to 1. If DST is enabled in e.g. 2:00, all times between 2:00 and 3:00 are invalid and mktime() returns an undefined (usually negative) value. Some systems (e.g. Solaris 8) enable DST at midnight so time 0:30 of the day when DST is enabled is evaluated as 23:30 of the previous day.

Note: As of PHP 5.1.0, this parameter became deprecated. As a result, the new timezone handling features should be used instead.
Note: This parameter has been removed in PHP 7.0.0.

Try the following code:

<?php

    $noSecs = mktime(date('03'), date('45'), date('28'), date('5'), date('9'), date('2023'));

    echo $noSecs;

?>

I tried it and I had:

    1683603928

mktime() returns the Unix timestamp of the arguments given. If the arguments are invalid, the function returns FALSE.

That is it for this part of the series. We continue in the next part.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments