Getting time and date from timestamp with php

Issue

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 am 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

Solution

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

Answered By – Andreas Wong

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published