Change date format from variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Change date format from variable

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Change date format from variable

Post by requinix »

blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Change date format from variable

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Change date format from variable

Post 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:
Last edited by social_experiment on Sun Jan 16, 2011 11:54 pm, edited 2 times in total.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Change date format from variable

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Change date format from variable

Post by requinix »

So exactly how much PHP do you know?

Code: Select all

date('M j, Y H:i', strtotime($post_date))
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Change date format from variable

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Change date format from variable

Post 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.
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Change date format from variable

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Change date format from variable

Post 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/
Last edited by Jonah Bron on Sun Jan 16, 2011 9:11 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Change date format from variable

Post by requinix »

Post Reply