get year, month, day, hour, minute, second with php | code time()

Technology

header ads

get year, month, day, hour, minute, second with php | code time()

get year, month, day, hour, minute, second with php ,code

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.



  
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);



  1.  date("d", $time); we will get the day. 
  2. date("m", $time); we will get the month.
  3. date("Y", $time); we will get the year
  4. date("h", $time); we will get the time
  5. date("i", $time); we will get the minute
  6. 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 =)

Post a Comment