/    Sign up×
Community /Pin to ProfileBookmark

convert 2011-01-06 22:46:40 to Jan 06, 2011 at 10:46 PM

I store my datetime values as, for example, “2011-01-06 22:46:40”, but want them to display as “Jan 06, 2011 at 10:46 PM”. I have fiddled around with PHP’s date functions but can’t get exactly what I am looking for, probably because I am a moron. Please help me!

Thanks,

>

B

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 07.2011 — If you are pulling them out of a MySQL database, use the DATE_FORMAT() function in your SQL statement to get the desired format (assuming it is a datetime or timestamp type column). Otherwise, you can convert it within PHP to a UNIX timestamp via the strtotime() function, then convert that value to the desired string format via date().
Copy linkTweet thisAlerts:
@DasherJan 07.2011 — echo date ("M j, Y g:i A");
Copy linkTweet thisAlerts:
@bubbisthedogauthorJan 07.2011 — Okay...

I tried

[code=php]$EveryOneDate = date("M j, Y g:i A", $Row['Date']);[/code]

and it produced "Dec 31, 1969 7:33 PM". The date is in 2011 and the other stuff is wrong... Where am I going wrong?
Copy linkTweet thisAlerts:
@bubbisthedogauthorJan 07.2011 — If you are pulling them out of a MySQL database, use the DATE_FORMAT() function in your SQL statement to get the desired format (assuming it is a datetime or timestamp type column). Otherwise, you can convert it within PHP to a UNIX timestamp via the strtotime() function, then convert that value to the desired string format via date().[/QUOTE]

nog, you've helped me with a lot of stuff, and i thank you for that. And this is no exception. In this case I would rather not convert the date/time in my SQL statements, and rather format them as they need to be printed using PHP. I tried many variations of
convert it within PHP to a UNIX timestamp via the strtotime() function, then convert that value to the desired string format via date()[/QUOTE]
The problem is that I cannot figure out how to get it to print exactly what I want to print. I really have tried; I just can't get it to work...

Thanks,

bubbis
Copy linkTweet thisAlerts:
@criterion9Jan 07.2011 — [code=php]
date($format,strtotime($date_string));
[/code]
Copy linkTweet thisAlerts:
@DasherJan 07.2011 — [code=php]
$query = SELECT UCASE( DATE_FORMAT(Publish_Up,'%b %d, %Y %l:%i %p'))AS publishDate FROM table WHERE ...";

//do some query stuff

echo $row['publishDate'];
[/code]


I have no problem formatting it in MySQL works great, likely faster than in PHP processor.
Copy linkTweet thisAlerts:
@NogDogJan 07.2011 — [code=php]
<?php
$input = "2011-01-06 22:46:40";
$output = date('M d, Y at h:i A', strtotime($input));
echo $output;
[/code]
Copy linkTweet thisAlerts:
@bubbisthedogauthorJan 07.2011 — nog, Criterion, THANKS for taking the time to help me. I was not including the strtotime() function obviously. Never really worked with converting dates in PHP so now I know!

bubbis
Copy linkTweet thisAlerts:
@bubbisthedogauthorJan 07.2011 — [code=php]
$query = SELECT UCASE( DATE_FORMAT(Publish_Up,'%b %d, %Y %l:%i %p'))AS publishDate FROM table WHERE ...";

//do some query stuff

echo $row['publishDate'];
[/code]


I have no problem formatting it in MySQL works great,[B] likely faster than in PHP processor.[/B][/QUOTE]


That, Dasher, I will keep in mind, and will explore in the future. :-) For now I'd rather just use a very simple PHP function to convert the date and leave the queries 'clean', if you will.

Thanks for taking the time to help me!

bubbis
Copy linkTweet thisAlerts:
@DasherJan 10.2011 — FYI - I have done MySQL queries that are nearly 1000 characters long. MySQL still processes them amazing fast. Formating text or date strings in MySQL is a piece of cake compared to some much more complex processes it can handle.
Copy linkTweet thisAlerts:
@bubbisthedogauthorJan 13.2011 — FYI - I have done MySQL queries that are nearly 1000 characters long. MySQL still processes them amazing fast. Formating text or date strings in MySQL is a piece of cake compared to some much more complex processes it can handle.[/QUOTE]

10-4. Thanks!
×

Success!

Help @bubbisthedog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...