Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert mysql timestamp into actual date and time?

I have dates stored in a mysql table, they are set to store as CURRENT TIMESTAMP in each row and are stored as follows:

2010-05-29 01:17:35 

but what i am trying to do is somehow use PHP to be able to seperate everything and create a date more like:

May 29 2010 1:17 AM

can anyone at least direct me in the right path that i should take. any help is greatly appreciated!

like image 796
mcbeav Avatar asked Apr 05 '11 04:04

mcbeav


People also ask

How do I convert timestamps to dates?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

How do I change datetime to date in MySQL?

Here is the query to convert from datetime to date in MySQL. mysql> select cast(ArrivalDatetime as Date) as Date from ConvertDateTimeToDate; The following is the output.

How do I insert date in YYYY-MM-DD format in MySQL?

Introduction to MySQL DATE data type This format is fixed and it is not possible to change it. For example, you may prefer to use mm-dd-yyyy format but you can't. Instead, you follow the standard date format and use the DATE_FORMAT function to format the date the way you want. MySQL uses 3 bytes to store a DATE value.


2 Answers

echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35')); 

http://php.net/manual/en/function.date.php

like image 93
deceze Avatar answered Oct 29 '22 04:10

deceze


You have two solutions :

  • Use strtotime() to parse the date to a timestamp, and date() to re-format it to a string
  • Or use the DateTime class


In PHP code, this would mean using :

echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35')); 

Or :

$dt = new DateTime('2010-05-29 01:17:35'); echo $dt->format('M j Y g:i A'); 


strtotime + date is the solution you'll see used the most ; but it is not the best solution : with those, you'll work on UNIX Timestamps, which means a limited range of dates (from 1970 to 2038, if using 32 bits integers).

ON the other hand, using the DateTime class, there will be no limit to the range of dates you can work with.

like image 45
Pascal MARTIN Avatar answered Oct 29 '22 06:10

Pascal MARTIN