Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 844
  • Last Modified:

PHP: Convert from MySQL timestamp (yyyymmddhhiiss) to date

In a database, signup times are stored as a timestamp field, which means the date is stored as follows:

20110307120000

I need to be able to display these using PHPs date function - is there an easy way to do this?

strtotime on this value just gives the value for 01/01/1970...

Thanks
0
freestate
Asked:
freestate
2 Solutions
 
Pratima PharandeCommented:
echo sprintf("%02s:%02s:%02s", substr($date,4,2),substr($date,6,2),substr($date,2,2));
0
 
Pratima PharandeCommented:
$date_format=date("m:d:y",$time_stamp); // formats timestamp in mm:dd:yy  
 
print $date_format; // results here ... 02 : 11 : 07
0
 
Andrej PirmanCommented:
Hi,
inside PHP you can convert simply with date() function:
$date_format=date("m:d:y",$time_stamp); // formats timestamp in mm:dd:yy

Open in new window

or inside MySQL, for example:
# $select = array();
# $select[] = "MONTH(timeStamp) AS theMonth";
# $select[] = "DAY(timeStamp) AS theDay";

...then print it out with PHP:
<?php
echo $results["theMonth"]."/".$results["theDay"];
?>

Open in new window

The rest you can fing in PHP docs under date() function or in MySQL docs under TIME or TIMESTAMP

 
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
freestateAuthor Commented:
Hi - sorry, but omitting strtotime just moves the problem to the other end of the spectrum and results in 19-01-2038 as the output date regardless of what time is entered.

ie.

$timestamp = "20110307121543";
echo date("d-m-Y", $timestamp);

(the other route, which gives 01-01-1970 was:

$timestamp = "20110307121543";
echo date("d-m-Y", strtotime($timestamp));

)

What I need, is 07-03-2011
0
 
Pratima PharandeCommented:
try this
$date = "20110307121543";

echo sprintf("%02s:%02s:%02s", substr($date,4,2),substr($date,6,2),substr($date,2,2));
0
 
Pratima PharandeCommented:
try this

for your output 07-03-2011
$date = "20110307121543";

echo sprintf("%02s-%02s-%02s", substr($date,6,2),substr($date,4,2),substr($date,0,4));
0
 
freestateAuthor Commented:
Hi,

That gives me the print out, but I'm actually passing this to another date function to give me the date in long form (March 7th, 2011) - is there a way of getting that output as a string so I can then pass it to date()?

Thanks
0
 
Chris HarteThaumaturgeCommented:
For clarification, the timestamp is the number of seconds since the beginning of the epoch. The value you have 20110307120000, is the date and time stored as a string in year month day format ie.

2011/03/07:12:00:00

you use date_parse() to get this.

print_r(date_parse("20110307120000"));
0
 
freestateAuthor Commented:
I'm aware that the UNIX timestamp is seconds since the beginning of the epoch - however the value provided is the value that gets stored in my MySQL timestamp field - does that mean my hosts have the server setup wrong?

Tried using 'date_parse' but am getting an undefined function error?

<?
$timestamp = "20110307121543";
echo date_parse($timestamp);
?>
0
 
freestateAuthor Commented:
Sorry - just noticed your print_r - however, still get the undefined function error...
0
 
freestateAuthor Commented:
OK -  so tweaking pratima_mcs's offering seems to do the trick:

$timestamp = "20110307121543";
$newdate = sprintf("%02s-%02s-%02s", substr($timestamp,0,4),substr($timestamp,4,2),substr($timestamp,6,2));
echo date('F d, Y', strtotime($newdate));

Open in new window

0
 
Pratima PharandeCommented:
glad to know it helps you
0
 
adeelshahidCommented:
just use this for date and time.

$date_time = (string)20110307120000;
$year = substr($date_time, 0, 4);
$month = substr($date_time, 4, 2);
$date = substr($date_time, 6, 2);
$hour = substr($date_time, 8, 2);
$minute = substr($date_time, 10, 2);
$second = substr($date_time, 12, 2);

$timestamp = mktime($hour, $minute, $second, $month, $date, $year);
echo date('Y-m-d H:i:s', $timestamp);
0
 
freestateAuthor Commented:
Had to adapt the suggested expert solution to get where I needed to be
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now