PHP - examples - 06 : date, time, filters, ...

Revision:


date and time

Today is 2024/09/28
Today is 2024.09.28
Today is 2024-09-28
Today is Saturday
The time is 06:21:38pm
The time is 09:21:38am

Oct 05
Oct 12
Oct 19
Oct 26
Nov 02
Nov 09


There are 93 days until the end of 2023.
code:
            <?php
                echo "Today is " . date("Y/m/d") . "< br>";
                echo "Today is " . date("Y.m.d") . "< br>";
                echo "Today is " . date("Y-m-d") . "< br>";
                echo "Today is " . date("l") . "< br>";
                echo "The time is " . date("h:i:sa") . "< br>";
                date_default_timezone_set("Asia/Shanghai");
                echo "The time is " . date("h:i:sa");
                echo ("< br>"); echo ("< br>");
            ?>

            <?php
                $startdate = strtotime("Saturday");
                $enddate = strtotime("+6 weeks", $startdate);
                while ($startdate < $enddate) {
                    echo date("M d", $startdate) . "< br>";
                    $startdate = strtotime("+1 week", $startdate);
                }
            ?>
             
            <?php
                echo (" < br>"); echo ("< br>");
                $d1=strtotime("December 31");
                $d2=ceil(($d1-time())/60/60/24);
                echo "There are " . $d2 ." days until the end of 2023.";
            ?>
        

PHP date string reference

a - am or pm
A - AM or PM
B - Swatch Internet time
d - day of the month, two digits with leading zeros; i.e., "01" to "31"
D - day of the week, textual, three letters; i.e., "Fri"
F - month, textual, long; i.e., "January"
g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
G - hour, 24-hour format without leading zeros; i.e., "0" to "23"
h - hour, 12-hour format; i.e. "01" to "12"
H - hour, 24-hour format; i.e. "00" to "23"
i - minutes; i.e. "00" to "59"
I - (capital i) "1" if Daylight Savings Time, "0" otherwise
j - day of the month without leading zeros; i.e., "1" to "31"
l - (lowercase 'L') day of the week, textual, long; i.e., "Friday"
L - boolean for whether it is a leap year; i.e., "0" or "1"
m - month; i.e. "01" to "12"
M - month, textual, 3 letters; i.e. "Jan"
n - month without leading zeros; i.e. "1" to "12"
r - RFC 822 formatted date; i.e. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP 4.0.4)
s - seconds; i.e. "00" to "59"
S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
t - number of days in the given month; i.e., "28" to "31"
T - Timezone setting of this machine; i.e. "MDT"
U - seconds since the epoch
w - day of the week, numeric, i.e., "0" (Sunday) to "6" (Saturday)
Y - year, 4 digits; i.e. "1999"
y - year, 2 digits; i.e. "99"
z - day of the year; i.e., "0" to "365"
Z - timezone offset in seconds (i.e. "-43200" to "43200").


date and time

different date formats for 'today is': 2024/09/29 ; 2024.09.29 ; 2024-09-29

@2010-2024

different time formats for current is: 09:21:38am ; 09:21:38am

New_York time zone: current time is 09:21:38pm
Shanghai: current time is 09:21:38am

created date is 2022-08-12 11:14:54am
created date is: 2022/04/12 15:30:00pm
tomorrow is: 2024-09-30 12:00:00am
next Saturday is: 2024-10-05
in three months it is: 2024-12-29

Oct 05
Oct 12
Oct 19
Oct 26
Nov 02
Nov 09

still -70 days to go till July 21.
codes:
            <?php
                echo "different date formats for 'today is': " .date("Y/m/d")." ; ".date("Y.m.d")." ; ".date("Y-m-d");
            ?><br><br>
            @2010-<?php echo date("Y")?><br><br>
            <?php echo "different time formats for current is: ".date("h:i:sa"). " ; ".date("H:i:sa"); ?><br><br>
            <?php date_default_timezone_set("America/New_York"); echo "New_York time zone: current time is "
            .date("h:i:sa");?><br>
            <?php date_default_timezone_set("Asia/Shanghai"); echo "Shanghai: current time is ".date("h:i:sa");?>
            <br><br>
            <?php $d=mktime(11, 14, 54, 8, 12, 2022); echo "created date is ".date("Y-m-d h:i:sa", $d) ?><br>
            <?php $d=strtotime("3:30pm April 12 2022"); echo "created date is: ". date("Y/m/d H:i:sa", $d) ?><br>
            <?php $d=strtotime("tomorrow"); echo "tomorrow is: ".date("Y-m-d h:i:sa", $d)."<br>"; ?>
            <?php $d=strtotime("next Saturday"); echo "next Saturday is: ".date("Y-m-d", $d)."<br>"; ?>
            <?php $d=strtotime("+ 3 months"); echo "in three months it is: ".date("Y-m-d", $d)."<br>"; ?><br>
            <?php
                $startdate = strtotime("Saturday");
                $enddate = strtotime("+ 6 weeks", $startdate);
                while($startdate < $enddate){
                    echo date("M d", $startdate)."<br>";
                    $startdate = strtotime("+1week", $startdate);
                }
            ?><br>
            <?php
                $d1 = strtotime("July 21");
                $d2 = ceil(($d1-time())/60/60/24);
                echo "still ".$d2." days to go till July 21.";
            ?>
        

timestamp

1727572898
tomorrow is 2024-09-30
15 days ago it was 2024-09-14

timestamp current time is 1727572898
timestamp of March, 05, 2014 03:19:18
codes:
            <?php
            // current timestamp
                echo time()."<br>";
            // increrase one day from current date
                echo "tomorrow is ".date("Y-m-d", time()+86400)."<br>";
            // decrease 15 days from current date
                echo "15 days ago it was ".date("Y-m-d", time()-(86400*15));
            ?>
            <?php
                $timestamp = time(); echo "timestamp current time is ".($timestamp)."<br>";
                $timestamp = 1394003958; echo "timestamp of ".date("F, d, Y h:i:s", $timestamp);
            ?>
        

PHP date references

checkdate() - validates a Gregorian date.
date_add() - adds an amount of days, months, years, hours, minutes and seconds to a date.
date_create_from_format() - returns a new DateTime object formatted according to the specified format.
date_create() - returns new DateTime object.
date_date_set() - sets a new date.
date_default_timezone_get() - returns the default timezone used by all date/time functions in a script.
date_default_timezone_set() - sets the default timezone used by all date/time functions in a script.
date_diff() - returns the difference between two dates.
date_format() - returns a date formatted according to a specified format.
date_get_last_errors() - returns the warnings and errors found while parsing a date/time string.
date_interval_create_from_date_string() - sets up a DateInterval from the relative parts of the string.
date_interval_format() - formats the interval.
date_isodate_set() - set a date according to the ISO 8601 standard.
date_modify() - modifies the timestamp.
date_offset_get() - returns the timezone offset.
date_parse_from_format() - returns an associative array with detailed info about given date formatted according to the specified format.
date_parse() - returns associative array with detailed info about a specified date.
date_sub() - subtracts an amount of days, months, years, hours, minutes and seconds from a date.
date_sun_info() - returns an array with information about sunset/sunrise and twilight begin/end for a specified day and location.
date_sunrise() - returns time of sunrise for a given day and location.
date_sunset() - returns time of sunset for a given day and location.
date_time_set() - sets the time.
date_timestamp_get() - returns the Unix timestamp representing the date.
date_timestamp_set() - sets the date and time based on an Unix timestamp.
date_timezone_get() - return time zone relative to given DateTime.
date_timezone_set() - sets the time zone for the DateTime object.
date() - formats a local date and time.
getdate() - returns date/time information of the timestamp or the current local date/time.
gettimeofday() - returns the current time.
gmdate() - formats a GMT/UTC date and time.
gmmktime() - get Unix timestamp for a GMT date.
gmstrftime() - formats a GMT/UTC date and time according to locale settings.
idate() - formats a local time/date as integer.
localtime() - returns the local time.
microtime() - return the current Unix timestamp with microseconds.
mktime() - returns the Unix timestamp for a date.
strftime() - formats a local time/date according to locale settings.
strptime() - parses a time/date generated with strftime().
strtotime() - parses an English textual datetime into a Unix timestamp.
time() - returns the current time as a Unix timestamp.
timezone_abbreviations_list() - returns associative array containing dst, offset and the timezone name.
timezone_identifiers_list() - returns an indexed array containing all defined timezone identifiers.
timezone_location_get() - returns the location information for a specified timezone.
timezone_name_from_abbr() - returns the timezone name from abbreviation.
timezone_name_get() - returns the name of the timezone.
timezone_offset_get() -returns the timezone offset from GMT.
timezone_open() - creates new DateTimeZone object.
timezone_transitions_get() - returns all transitions for the timezone.
timezone_version_get() - returns the current version of the timezonedb.


filters

Filter Name Filter ID
int257
boolean258
float259
validate_regexp272
validate_domain277
validate_url273
validate_email274
validate_ip275
validate_mac276
string513
stripped513
encoded514
special_chars515
full_special_chars522
unsafe_raw516
email517
url518
number_int519
number_float520
add_slashes523
callback1024


Hello World!

Integer is valid

127.0.0.1 is a valid IP address

john.doe@example.com is a valid email address

https://www.lwitters.com is a valid URL
code:
            <div>
                <table>
                    <tr>
                        <td>Filter Name</td>
                        <td>Filter ID</td>
                    </tr>
                    <?php
                        foreach (filter_list() as $id =>$filter) {
                            echo '<tr><td>' . $filter . '</td><td>' . filter_id($filter) . '</td></tr>';
                    }
                    ?>
                </table>
                <?php
                    echo "<br>";  echo "<br>";
                    $str = "<h3>Hello World!</h3>";
                    $newstr = filter_var($str, FILTER_SANITIZE_STRING);
                    echo $newstr;
                    echo "<br>";  echo "<br>";

                    $int = 100;
                    if (!filter_var($int, FILTER_VALIDATE_INT) === false) {
                    echo("Integer is valid");
                    } else {
                    echo("Integer is not valid");
                    }
                    echo "<br>";
                    echo "<br>";

                    $ip = "127.0.0.1";
                    if (!filter_var($ip, FILTER_VALIDATE_IP) === false) {
                    echo("$ip is a valid IP address");
                    } else {
                    echo("$ip is not a valid IP address");
                    }
                    echo "<br>";  echo "<br>";

                    $email = "john.doe@example.com";
                    // Remove all illegal characters from email
                    $email = filter_var($email, FILTER_SANITIZE_EMAIL);
                    // Validate e-mail
                    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
                    echo("$email is a valid email address");
                    } else {
                    echo("$email is not a valid email address");
                    }
                    echo "<br>";  echo "<br>";

                    $url = "https://www.lwitters.com";
                    // Remove all illegal characters from a url
                    $url = filter_var($url, FILTER_SANITIZE_URL);
                    // Validate url
                    if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
                    echo("$url is a valid URL");
                    } else {
                    echo("$url is not a valid URL");
                    }
                ?>
            </div>