Page 1 of 1

Change date format from variable

Posted: Sat Jan 15, 2011 3:09 pm
by blade_922
Hi,

I have the date and stored in $date variable. It appears as 2010-10-22 17:21:01 what i want to do is turn this format into this: Oct 22, 2010 17:21

I have no clue how to do this, searching google just confuses me more. If someone can provide a simple solution to change the format and output it to a variable which will then be stored in my DB.

Re: Change date format from variable

Posted: Sat Jan 15, 2011 7:07 pm
by requinix

Re: Change date format from variable

Posted: Sun Jan 16, 2011 7:50 am
by blade_922
tasairis wrote:strtotime + date.

Okay so

Code: Select all

<?php strtotime ( string $time [, int $post_time] ) string date ( string $format [, int $post_date] )?>

Okay so $post_time is the time appearing as 17:21:01 and the date is $post_date appearing as 2010-10-22

I know the above wont work, im completely baffled as to how to convert the date!

Can someone assist me further.

Kind Regards
Omar

Re: Change date format from variable

Posted: Sun Jan 16, 2011 9:30 am
by social_experiment
strtotime() converts your existing timestamp to a unix timestamp and then date() is used to get it in the order you want (Hint : date('M j, Y H:i', $yourStrTotimeValue) ).
EDIT : fixed the parameter order :oops:

Re: Change date format from variable

Posted: Sun Jan 16, 2011 11:53 am
by blade_922
social_experiment wrote:strtotime() converts your existing timestamp to a unix timestamp and then date() is used to get it in the order you want (Hint : date($yourStrTotimeValue, 'M j, Y H:i') ).
I understand the part where i can get it in the order i want, BUT its all the strings and variables slightly baffling me. Im down to guessing now, but it keeps giving me errors :cry:

This is what i've put in so far

Code: Select all

strtotime($post_date) 

$date($post_date, 'M j, Y H:i');

lol that above means nothing to me :?

Help! :banghead: :banghead: :banghead:

Im confused as to how to input the $post_date variable into the part wher it converts the date then outputs it as another variable.

Re: Change date format from variable

Posted: Sun Jan 16, 2011 1:40 pm
by requinix
So exactly how much PHP do you know?

Code: Select all

date('M j, Y H:i', strtotime($post_date))

Re: Change date format from variable

Posted: Sun Jan 16, 2011 2:53 pm
by blade_922
tasairis wrote:So exactly how much PHP do you know?

Code: Select all

date('M j, Y H:i', strtotime($post_date))
I'm learning little by little.

So to understand what im using

Code: Select all

date('M j, Y H:i', strtotime($post_date))
The $post_date variable can still be used and will display the new format or will the new format of the date and time be in a different variable?

Re: Change date format from variable

Posted: Sun Jan 16, 2011 3:59 pm
by requinix
Depends what you do with the code. I didn't give an entire statement - you're supposed to assign the result back to a variable ($post_date or something else, doesn't matter) or immediately print it out or whatever you want to do.

Re: Change date format from variable

Posted: Sun Jan 16, 2011 4:24 pm
by blade_922
tasairis wrote:Depends what you do with the code. I didn't give an entire statement - you're supposed to assign the result back to a variable ($post_date or something else, doesn't matter) or immediately print it out or whatever you want to do.

um, and how would i do that exactly? Im inserting the new format of the date into my database. Just unsure on how to place it/assign to variable.

Re: Change date format from variable

Posted: Sun Jan 16, 2011 9:09 pm
by Jonah Bron
Here you go.

Code: Select all

$post_date = date('M j, Y H:i', strtotime($post_date));
First, it passes the value of $post_date to strtotime() as the first (and only) argument. Strtotime() returns it's output, and that is passed as the second argument to date(). The first argument of date() is a string defining the format of the date wanted, according to the syntax defined here: http://php.net/date

You really should read a tutorial on the basics of PHP.

http://w3schools.com/php/

Re: Change date format from variable

Posted: Sun Jan 16, 2011 9:09 pm
by requinix