FreeKB - PHP date format
PHP - date format

The built in PHP date function is used to produce a formatted date. For example, here is how to print the current year.

<?php
   echo date("Y");
?>

 

Here are commonly used format options.

format label result example
date("Y") year yyyy 2022
date("m") month mm 01
date("M") month mmm Jan
date("d") day dd 09
date("H") hour hh 07
date("i") minutes mm 45
date("s") seconds ss 13

 


Epoch time

strtotime can be used to convert a date and time into epoch time.

$current_epoch_time = strtotime(date("Y-m-d H:i:s"));

 


mySQL / MariaDB (date only)

Let's say you have created a table named foo in mySQL or MariaDB that contains a date column named date.

create table foo ( date date );

 

With the date column setup in this way, the date must be in the yyyymmdd format.

insert into db001.foo (date) values ('20220102')

 

Or in the yyyy-mm-dd format.

insert into db001.foo (date) values ('2022-01-02')

 

Here is an example of how you could insert a record into the foo table using PDO.

$sql = "insert into foo (date) values (:date)";
$date = date("Y-m-d");
$stmt->bindParam(':date', $date, PDO::PARAM_STR);
$stmt->execute();

 

Regardless if the date was inserted as yyyymmdd or as yyyy-mm-dd, in the table, the date will be formatted as yyyy-mm-dd.

~]$ mysql -e "select * from db001.foo \G"
*************************** 1. row ***************************
date: 2022-01-09

 


Date and Time

If you want to capture both the date and the time, when creating the mySQL or MariaDB table, you will use datetime instead of "date".

create table foo ( datetime datetime );

 

When inserting into the table, only the date can be specified.

$sql = "insert into foo (datetime) values (:datetime)";
$datetime = date("Y-m-d");
$stmt->bindParam(':datetime', $datetime, PDO::PARAM_STR);
$stmt->execute();

 

If only the date is specified, then the time will be 00:00:00.

~]$ mysql -e "select * from db001.foo \G"
*************************** 1. row ***************************
datetime: 2022-01-09 00:00:00

 

Or the date and time can be specified.

$sql = "insert into foo (datetime) values (:datetime)";
$datetime = date("Y-m-d H:i:s");
$stmt->bindParam(':datetime', $datetime, PDO::PARAM_STR);
$stmt->execute();

 

To include the time.

~]$ mysql -e "select * from db001.foo \G"
*************************** 1. row ***************************
datetime: 2022-01-09 07:25:16

 


mySQL / MariaDB SELECT date

Here is an example of selecting data from the foo table.

foreach($con->query("select * from foo") as $row) {
  echo $row['datetime'];
}

 

This will print the date exactly as it is in the datetime column in the foo table.

2022-01-09 07:25:16

 

However, you can format the date.

foreach($con->query("select * from foo") as $row) {
  echo $row['datetime']->format('m/d/Y');
}

 


Timezone

You will want to ensure that your php.ini file has the appropriate time zone, like this. If you make a change in php.ini, restart the php service.

date.timezone = America/Chicago

 

The following markup can be used to determine if date.timezone has been set properly.

date_default_timezone_set('America/Chicago');

$timezone = date_default_timezone_get();

if (strcmp($timezone, ini_get('date.timezone'))){
    echo 'date.timezone in php.ini is not America/Chicago';
}
else {
    echo 'date.timezone in php.ini is America/Chicago';
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 878e0 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |