Jump to content

formatting dates and times


paul.mearing

Recommended Posts

[!--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) [snapback]372568[/snapback][/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
Link to comment
Share on other sites

[!--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) [snapback]372581[/snapback][/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:
[code]$open_date1 = strtotime("Y-m-d I:Mr", strtotime($open_date));[/code]
Got any ideas?
Link to comment
Share on other sites

[!--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) [snapback]372626[/snapback][/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:
[code]$open_date1 = strtotime("Y-m-d I:Mr", strtotime($open_date));[/code]
Got any ideas?
[/quote]

you're close, but he's recommended using strftime() on the outside, not strtotime(). also, you could use date():
[code]
$open_date1 = date("Y-m-d I:Mr", strtotime($open_date));
[/code]
Link to comment
Share on other sites

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


When i try to use:

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

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
Link to comment
Share on other sites

[!--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) [snapback]372632[/snapback][/div][div class=\'quotemain\'][!--quotec--]
When i try to use:

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

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) [snapback]372581[/snapback][/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:
[code]$open_date1 = strftime("Y-m-d I:Mr", strtotime(substr($open_date, 0, strrpos($open_date, "."))));[/code]

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 [i]does[/i] help [i]a lot[/i].

hth
RT
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.