Jump to content


Photo

formatting dates and times


  • Please log in to reply
5 replies to this topic

#1 paul.mearing

paul.mearing
  • Members
  • Pip
  • Newbie
  • 5 posts

Posted 09 May 2006 - 01:11 PM

Hi,

I have outputs from a sql database which print out in this format:
2006-05-09 18:11:20.000

is there a simple way i can format it so it looks like this:
2006-05-09 6:20pm

Any ideas?

Thanks.

#2 rt-box

rt-box
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationParis, France

Posted 09 May 2006 - 01:42 PM

[!--quoteo(post=372568:date=May 9 2006, 03:11 PM:name=paul.mearing)--][div class=\'quotetop\']QUOTE(paul.mearing @ May 9 2006, 03:11 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi,

I have outputs from a sql database which print out in this format:
2006-05-09 18:11:20.000

is there a simple way i can format it so it looks like this:
2006-05-09 6:20pm

Any ideas?

Thanks.
[/quote]

$output = strftime("Y-m-d I:Mr", strtotime($date_from_db));

In versions prior to PHP 5, you'd also have to get rid of the microseconds in the time you get from the database, as it will generate an error in strtotime().

For further precision, read the documentation.

hth

#3 paul.mearing

paul.mearing
  • Members
  • Pip
  • Newbie
  • 5 posts

Posted 09 May 2006 - 03:10 PM

[!--quoteo(post=372581:date=May 9 2006, 02:42 PM:name=rt-box)--][div class=\'quotetop\']QUOTE(rt-box @ May 9 2006, 02:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$output = strftime("Y-m-d I:Mr", strtotime($date_from_db));

In versions prior to PHP 5, you'd also have to get rid of the microseconds in the time you get from the database, as it will generate an error in strtotime().

For further precision, read the documentation.

hth
[/quote]


When i try what you should, I get all my results echo'ing out as "-1". This is my code:
$open_date1 = strtotime("Y-m-d I:Mr", strtotime($open_date));
Got any ideas?

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 09 May 2006 - 03:12 PM

[!--quoteo(post=372626:date=May 9 2006, 11:10 AM:name=paul.mearing)--][div class=\'quotetop\']QUOTE(paul.mearing @ May 9 2006, 11:10 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
When i try what you should, I get all my results echo'ing out as "-1". This is my code:
$open_date1 = strtotime("Y-m-d I:Mr", strtotime($open_date));
Got any ideas?
[/quote]

you're close, but he's recommended using strftime() on the outside, not strtotime(). also, you could use date():
$open_date1 = date("Y-m-d I:Mr", strtotime($open_date));

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 paul.mearing

paul.mearing
  • Members
  • Pip
  • Newbie
  • 5 posts

Posted 09 May 2006 - 03:28 PM

[!--quoteo(post=372628:date=May 9 2006, 04:12 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 9 2006, 04:12 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
you're close, but he's recommended using strftime() on the outside, not strtotime(). also, you could use date():
$open_date1 = date("Y-m-d I:Mr", strtotime($open_date));
[/quote]


When i try to use:

$open_date1 = strftime("Y-m-d I:Mr", strtotime($open_date));

i get blanks results...when i try it with date(), i get the following error:

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\inetpub\wwwroot\magic8\SevBoard\PHPSevboard\sev123brd.php on line 86

#6 rt-box

rt-box
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationParis, France

Posted 09 May 2006 - 08:18 PM

[!--quoteo(post=372632:date=May 9 2006, 05:28 PM:name=paul.mearing)--][div class=\'quotetop\']QUOTE(paul.mearing @ May 9 2006, 05:28 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
When i try to use:

$open_date1 = strftime("Y-m-d I:Mr", strtotime($open_date));

i get blanks results...when i try it with date(), i get the following error:

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\inetpub\wwwroot\magic8\SevBoard\PHPSevboard\sev123brd.php on line 86
[/quote]

As I said in my first reply:
[!--quoteo(post=372581:date=May 9 2006, 02:42 PM:name=rt-box)--][div class=\'quotetop\']QUOTE(rt-box @ May 9 2006, 02:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]In versions prior to PHP 5, you'd also have to get rid of the microseconds in the time you get from the database, as it will generate an error in strtotime().[/quote]
So now you know: check the version of PHP you are using, and if prior to version 5, remove the microseconds from the $open_date string:
$open_date1 = strftime("Y-m-d I:Mr", strtotime(substr($open_date, 0, strrpos($open_date, "."))));

If I may add - no offense - please read twice and check all the indications given before reposting. And also, do check the [a href=\"http://www.php.net/docs.net\" target=\"_blank\"]PHP reference[/a] before posting as it does help a lot.

hth
RT




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users