Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting time and date from timestamp with php

Tags:

in my database I have a time stamp column...which reflects a format like this: 2012-04-02 02:57:54

However I would like to separate them up into $date and $time.

after some research through the php manual...I found that date() , date_format() and strtotime() are able to help me to separate them...(not sure if I am right)

but I not very sure of how to code it out...

In my php file...the timestamp extracted would be $row['DATETIMEAPP'].

Will

$date= strtotime('d-m-Y',$row['DATETIMEAPP']); $time= strtotime('Gi.s',$row['DATETIMEAPP']); 

or

$date= date('d-m-Y',$row['DATETIMEAPP']); 

work?

Can i use date() to get the time as well??

Thanks in advance

like image 690
Hubert Avatar asked Apr 02 '12 03:04

Hubert


People also ask

How can I get current date and time in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How can I get current date in YYYY-MM-DD format in PHP?

$date = date("yyyy-mm-dd", strtotime(now));

How get fetch time from database in PHP?

PHP time() Functionecho(date("Y-m-d",$t));


1 Answers

$timestamp = strtotime($row['DATETIMEAPP']); 

gives you timestamp, which then you can use date to format:

$date = date('d-m-Y', $timestamp); $time = date('Gi.s', $timestamp); 

Alternatively

list($date, $time) = explode('|', date('d-m-Y|Gi.s', $timestamp)); 
like image 178
Andreas Wong Avatar answered Oct 09 '22 08:10

Andreas Wong