26

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

Martin54
  • 1,349
  • 2
  • 13
  • 34
Hubert
  • 443
  • 1
  • 8
  • 20
  • Why is this question not useful?? just wanna know which is best way to get the 2 value out from timestamp – Hubert Apr 02 '12 at 03:37
  • possible duplicate of [How to extract only 'Day' value from full 'Date' string?](http://stackoverflow.com/questions/4275599/how-to-extract-only-day-value-from-full-date-string) – deceze Apr 02 '12 at 03:41
  • i can't try because i have other errors on the page...i want to know if this plays a part in causing the error...Thanks – Hubert Apr 02 '12 at 03:41
  • @deceze i thought that was to convert a date to time stamp? – Hubert Apr 02 '12 at 03:44
  • No, that's one example of how to extract the DD part of a YYYY-MM-DD date string. And many similar such questions have been posted many times already. – deceze Apr 02 '12 at 03:47

8 Answers8

71
$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));
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
10

If you dont want to change the format of date and time from the timestamp, you can use the explode function in php

$timestamp = "2012-04-02 02:57:54"
$datetime = explode(" ",$timestamp);
$date = $datetime[0];
$time = $datetime[1];
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ankit Bansal
  • 2,162
  • 8
  • 42
  • 79
2
$mydatetime = "2012-04-02 02:57:54";
$datetimearray = explode(" ", $mydatetime);
$date = $datetimearray[0];
$time = $datetimearray[1];
$reformatted_date = date('d-m-Y',strtotime($date));
$reformatted_time = date('Gi.s',strtotime($time));
2

You can try this:

For Date:

$date = new DateTime($from_date);
$date = $date->format('d-m-Y');

For Time:

$time = new DateTime($from_date);
$time = $time->format('H:i:s');
Tushar Nitave
  • 519
  • 4
  • 13
1
$timestamp='2014-11-21 16:38:00';

list($date,$time)=explode(' ',$timestamp);

// just time

preg_match("/ (\d\d:\d\d):\d\d$/",$timestamp,$match);
echo "\n<br>".$match[1];
zzapper
  • 4,743
  • 5
  • 48
  • 45
0

Works for me:

select DATE( FROM_UNIXTIME( columnname ) ) from tablename;
0

If you want to use the DateTime class, you can do so like this:

$timestamp = $row['DATETIMEAPP']; // String formatted as "2012-04-02 02:57:54"

// Create DateTime object from custom timestamp
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $timestamp);

$date = $dt->format('d-m-Y'); // String variable of just the date
$time = $dt->format('H:i:s'); // String variable of just the time

And if you're concerned about using DateTime over strtotime() or date(), I'd like to point you in the direction of this conversation on StackOverflow titled "DateTime class vs. native PHP date-functions."

AuRise
  • 2,253
  • 19
  • 33
-1

Optionally you can use database function for date/time formatting. For example in MySQL query use:

SELECT DATE_FORMAT(DATETIMEAPP,'%d-%m-%Y') AS date, DATE_FORMT(DATETIMEAPP,'%H:%i:%s') AS time FROM yourtable

I think that over databases provides solutions for date formatting too

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39