get the year, month, day, hour, minute, second with php code easy
for the programming and php MBA data of dates is very necessary, especially when using any database manager such as MySQL, PostgreSQL, Oracle, etc.
and have the exact time of a record in a tuple, here on this web page we will teach you how to take the exact date, to be able to register it in the database or show it on the page.
the first thing to do is call the variable date_default_timezone_set this variable is responsible for obtaining the time zone of the region in this case we are going to use America / Los_Angeles.
and have the exact time of a record in a tuple, here on this web page we will teach you how to take the exact date, to be able to register it in the database or show it on the page.
the first thing to do is call the variable date_default_timezone_set this variable is responsible for obtaining the time zone of the region in this case we are going to use America / Los_Angeles.
date_default_timezone_set('America/Los_Angeles');
So we will tell you to take the region of the angels if you want to know more about this variable here:
date_default_timezone_set php.net
we just need to call the variable time (), which will give us the exact time of the server to which we consulted and we will obtain in several variables the year, month, day, hour, minute, second with its respective letter:
$time = time();
$day = date("d", $time);
$month = date("m", $time);
$year = date("Y", $time);
$hour = date("h", $time);
$minute = date("i", $time);
$second = date("s", $time);
- date("d", $time); we will get the day.
- date("m", $time); we will get the month.
- date("Y", $time); we will get the year
- date("h", $time); we will get the time
- date("i", $time); we will get the minute
- date("s", $time); we will get the second
In this way we can store it in a string, concatenate it and show it on an exact date:
$todaydate = '' . $year . '-' . $month . '-' . $day. ' ' . $hour.':'.$minute.':'.$second;
and finally all the code as it would be:
<?php
date_default_timezone_set('America/Los_Angeles');
$time = time();
$day = date("d", $time);
$month = date("m", $time);
$year = date("Y", $time);
$hour = date("h", $time);
$minute = date("i", $time);
$second = date("s", $time);
$todaydate = '' . $year . '-' . $month . '-' . $day. ' '
. $hour.':'.$minute.':'.$second;
echo "today date is: ".$todaydate;
?>
if you liked it you can leave a comment or share this post with a friend programmer =)
0 Comments