Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

give out a date in formation like this: 20040525230914

Status
Not open for further replies.

lukelukeluke

Technical User
Joined
Dec 23, 2003
Messages
117
Location
CH
Hi there, i have problems with my date.
I used timestamp() for some entrys into my database. What timestamp made was a code of the current date. This code looks like this: 20040525230914.

I would like to give out this code now a bit bether, Like this: "May, 25. 2004".
How can i do that? I have tryed many things but all wont work. Can anyone help me?

here is something i tryed:
Code:
<?php
$time = "20040525230914";
$ts = strtotime ($time);
print(date("20040525230914"));
echo("<br><br>");
print (date ("h:i A l F dS, Y", $ts));
echo("<br><br>");

$dasdatum = getdate($time);
print (date ($dasdatum));
?>
 
timestamp() is not a PHP function, so I'm guessing you're referring a function specific to your database implementation (which you didn't provide). If so, there's a good chance your DB can go ahead and convert things directly back for you.

Otherwise... I'd do it like so...
Code:
$time='20040525230914';

$pattern='/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/';

preg_match($pattern, $str, $matches);

$year=$matches[1];
$month=$matches[2];
$day=$matches[3];
$hour=$matches[4];
$minute=$matches[5];
$second=$matches[6];

And from there you can ahead and use whichever PHP time/date functions you like
 
Try something like this:

Code:
echo date("F d, Y", timestamp());
 
i see how youre solution works. But i think thats the harder way.
My database is mysql.
i used this code to enter the date:
datum timestamp(14) NOT NULL,
 
Just thought of another idea... saves a couple steps in that you don't extract the information and then feed it to the date functions...

Code:
$read_pattern='/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/';
$write_pattern='$2-$3-$1 $4:$5:$6';

$php_timestamp = preg_replace($read_pattern, $write_pattern, $str);
$time= strtotime($php_timestamp);
echo date("Y-m-d H:i:s");

Still, probably greatly more complex than using the DATE() function in mysql... but it definitely does the job.
 
Correction
Code:
$write_pattern='$1-$2-$3 $4:$5:$6';
 
I would recommend to use the MySQL function date_format():

MySQL stores it internally in the selected column type. You can retrieve it from the table in a select statement and format it at the same time. The result will contain the formatted date as a string.
 
Feedback:

Thank you very much for all of youre ideas!
I finally made it with an easyer idea, a php function, no mysql function. I took the timecode and made this:

echo substr($timecode, 0, 4);

this takes 4 numbers starting from 0....
 
$MyDateConverted = date("j of F Y", $someTimeStamp);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top