1

I get php output function from another function and it always returns date in this-like format:

2012-05-24 09:05:20

So my question is how can i get from variable $p_re["last_login"] format I need? I need to have it like

24.5.2012 09:05:20

Thank you all I did not find it in any section there are some - changing right from database but i need to "rewrite" format from already output variable. Thank you.

1
  • what is your function? what code did you write? put some thing so that one can suggest you possible ways.. Commented May 24, 2012 at 8:29

4 Answers 4

7

Try This code just before displaying date on page

$p_re["last_login"]  = date('d.m.Y H:i:s', strtotime($p_re["last_login"]));

More info

'd.m.Y' will return 24.05.2012

'd.n.Y' will return 24.5.2012

Sign up to request clarification or add additional context in comments.

8 Comments

This is perfect, thank you ... And if I want to show numbers without "0" (zero) at the begining I just change letters/chatacters, right?
j.n.Y gives you 4.7.2012 instead of 04.07.2012, just in case you don't want leading zeros for days neither.
And one last - If I want to show name of months? Like May or December in my language I use what? :)
j. F Y shows the month name. To have it in your language, you need to set locale info. For example, to have it in Dutch, you need to call setlocale (LC_TIME, 'nl_NL');.
Locale info in my code or in php settings? Thank you for everything
|
2

General answer for custom time formats (needs adjustments):

$output = "2012-05-24 09:05:20";

$converted = date("d.m.Y H:i:s", mktime(substr($output,11,2), substr($output,14,2), substr($output,17,2), substr($output,5,2), substr($output,8,2), substr($output,0,4)));

Since your output is standard MySQL compound format, you may use

$output = "2012-05-24 09:05:20";

$converted = date("d.m.Y H:i:s", strtotime($output));

Instead of d.m.Y you can use d.n.Y if you want 24.5.2012 and not 24.05.2012 (Note the leading zero in month).

Comments

0

A more customisable approach is to use the DateTime object.

Start with making a DateTime object withDateTime::createFromFormat and then output that with DateTime::format.

Comments

0
$date_str = '2012-05-24 09:05:20';

You can use below statement to format your date according to your desire.

$formatted_date = date('Y.m.d H:i:s', strtotime($date_str))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.