QUIZGUM

Coding Class

Quizgum : date

Date and time function

Date and time information output function: date()

String Meanings Output

A

morning
afternoon

Capitalize morning and afternoon
To display in lowercase letters a

A : AM,PM
a : am, pm

D

Day of the week

Day of week in three letters

Sun ~ Sat

F

month

Month in full characters

January ~ December

h

hour

12 hour indication with 0
(g : 12 hours without 0)

h : 0 ~ 12
g : 1 ~ 12

H

hour

24 hour indication with 0
(G : 24 hours without 0)

H : 00 ~ 23
G : 1 ~ 23

i

minute

minute with 0

00 ~ 59

j

day

day without 0
(d : day with 0)

j : 1 ~ 31
d : 01 ~ 31

l

day of week

Day of week in full text

Sunday ~ Saturday

M

month

Month in three letters

Jan ~ Dec

n

month

month without 0
m : month with 0

n : 1 ~ 12
m : 01 ~ 12

s

second

second with 0

00 ~ 59

t

number of Day of month

Display number of days in the month

28 ~ 31

Y

year

Four letter year
y : two letter year

Y : 2024
y : 24

z

Days Since Year

The day of the year

0 ~ 365

<?php
    echo "Let's print today's date and time.<br />";

    $today1 = date("Y年n月j日(D曜日)");
    $today2 = date("l / d / F / Y ");
    echo "today... {$today1} <br />";
    echo "display with english ... {$today2} <br />";

    $now = date("h時 i分 s秒(A)");
    echo "current time is .... {$now}<br />";

    echo "=================================== <br />";
    $cnt = date("z");
    $tocnt = $cnt + 1; // Starts at 0, so add 1
    echo "{$tocnt} day has started since this year<br />";

    $mon = date("n");
    $mck = date("t");
    echo "This month, {$mon} month, is up to {$mck} days.<br />";
    echo "----------------------------------- <br />";
?>

Result

php date image

Function to calculate time time() ,mktime()


These two functions calculate the time passed in seconds, based on 0: 0: 0 seconds on January 1, 1970.
The time function can not set arguments.
The mktime() function can calculate the time from 1970 to the time you enter by setting the time you want to calculate in the argument.

<?php
    $a = time(); // The value a is the time from January 1, 1970 to the present.
    $b = mktime(8, 10, 20, 2, 1, 2020); // 2020-2-1 8:10:20, year-month-day hour:minute:second
    $c = $a - $b; // The time from 1970 until now is subtracted from 5:10 to 20 seconds from 1970 to May 3, 200.
    $d = intval($c / 86400); //  Divide c's time by one day.

    echo "1970-1-1 00:00:00 standard <br />";

    echo "It's been {$a}seconds so far<br />";
    echo "It's been {$d}days until your birthday. <br />";
?>
php image

It is a source that finds a period of two time points by specifying a specific date as a start date and a specific date as an end date.

`
<?php
    $start = mktime(8, 35, 20, 1, 16, 2000); // start day is 2000-1-16 8:35:20
    $std1 = date("from Y-m-d h:i:s",$start);
    $end = mktime(5, 35, 20, 1, 16, 2009);
    $std2 = date("to Y-m-d h:i:s", $end);
    $count = $end - $start; // end - start
    $total1 = intval($count/86400); // per one day
    $total2 = intval(($count % 86400) / 3600);

    echo " mktime date<br />";
    echo " start : {$std1}, ";
    echo " end : {$std2}";
    echo "{$count}seconds have passed since the start date. <br />";
    echo "In terms of dates, {$total1} days and {$total2} hours have passed.<br />";
?>
php image

Function to validate dates : checkdate()

This function validates whether the date format is entered correctly.
True if the format is correct, false if it is not.

<?php
    echo "Function to validate dates<br />";
    $m = 06;
    $d = 3;
    $y = 2009;
    echo "input date : {$y}.{$m}.{$d} <br />";
    $chk = checkdate($m, $d ,$y);
    if($chk){
        echo "The format of the date is correct.<br />";
    }else{
        echo "Date is not in the correct format. <br />";
    }
?>
php image

The format mismatch in the above source is false if the month sets the month of June and the date is up to 30 days a year in June.