470,855 Members | 1,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

FormatDateTime in PHP

I recently took a job doing PHP but come from an ASP background. I'm
trying to find a conversion equivalent for VBScript's "formatdatetime".

All I want to do is convert a MySQL datetime field into a long date.
e.g. 2006-01-30 to January 1, 2006.
PLEASE provide an example that I can use as a future benchmark so I can

understand the syntax better. I really need to learn PHP and I'm a
little freaked out right now.
Thanks!

Jan 5 '06 #1
10 3457
John K wrote:
I recently took a job doing PHP but come from an ASP background. I'm
trying to find a conversion equivalent for VBScript's "formatdatetime".

All I want to do is convert a MySQL datetime field into a long date.
e.g. 2006-01-30 to January 1, 2006.
PLEASE provide an example that I can use as a future benchmark so I can

understand the syntax better. I really need to learn PHP and I'm a
little freaked out right now.
Thanks!


2006-01-30 can be converted to "January 1, 2006" using
date("F j, Y",mktime(0,0,0,01,30,2006));
Or, you can accomplish the same task with variables stuck into the mktime
function:
date("F j, Y",mktime($hour,$minute,$second,$month,$day,$year) );

Kevin
Jan 5 '06 #2
http://www.php.net/manual/en/ is a great place to start learning PHP.
Also, they have a function search on that page which allows you to
search for functions... The search input field has an auto-complete
feature to help you find the function you wish to locate.

Good Luck at your new job!

Scot

John K wrote:
I recently took a job doing PHP but come from an ASP background. I'm
trying to find a conversion equivalent for VBScript's "formatdatetime".

All I want to do is convert a MySQL datetime field into a long date.
e.g. 2006-01-30 to January 1, 2006.
PLEASE provide an example that I can use as a future benchmark so I can

understand the syntax better. I really need to learn PHP and I'm a
little freaked out right now.
Thanks!


--
Scot McConnaughay
Jan 5 '06 #3

John K wrote:
All I want to do is convert a MySQL datetime field into a long date.
e.g. 2006-01-30 to January 1, 2006.


I recently asked a similar question. See if this thread helps:
http://groups.google.com/group/comp....00282f2d660cca

greg

Jan 5 '06 #4
This is how I'm trying to do this at the moment, I should have included
this first, sorry.

<?php require_once('Connections/hthConnect.php'); ?>
<?php
mysql_select_db($database_hthConnect, $hthConnect);
$query_gigRecall = "SELECT tbl_gigs.fld_gig_Date,
tbl_gigs.fld_gig_Time, tbl_gigs.fld_gig_Title, tbl_gigs.fld_gig_Place
FROM tbl_gigs WHERE tbl_gigs.fld_gig_Date >= CURDATE()";
$gigRecall = mysql_query($query_gigRecall, $hthConnect) or
die(mysql_error());
$row_gigRecall = mysql_fetch_assoc($gigRecall);
$totalRows_gigRecall = mysql_num_rows($gigRecall);
?>
<span class="midYellowCaps"><?php echo $row_gigRecall['fld_gig_Title'];
?></span><br>
HERE WHERE I'M TRYING TO FORMAT THE DATE BELOW
<?php echo $row_gigRecall['fld_gig_Date']; ?> at <?php echo
$row_gigRecall['fld_gig_Time']; ?>

Jan 5 '06 #5
John K wrote:
This is how I'm trying to do this at the moment, I should have included
this first, sorry.

<?php require_once('Connections/hthConnect.php'); ?>
<?php
mysql_select_db($database_hthConnect, $hthConnect);
$query_gigRecall = "SELECT tbl_gigs.fld_gig_Date,
tbl_gigs.fld_gig_Time, tbl_gigs.fld_gig_Title, tbl_gigs.fld_gig_Place
FROM tbl_gigs WHERE tbl_gigs.fld_gig_Date >= CURDATE()";
$gigRecall = mysql_query($query_gigRecall, $hthConnect) or
die(mysql_error());
$row_gigRecall = mysql_fetch_assoc($gigRecall);
$totalRows_gigRecall = mysql_num_rows($gigRecall);
?>
<span class="midYellowCaps"><?php echo $row_gigRecall['fld_gig_Title'];
?></span><br>
HERE WHERE I'M TRYING TO FORMAT THE DATE BELOW
<?php echo $row_gigRecall['fld_gig_Date']; ?> at <?php echo
$row_gigRecall['fld_gig_Time']; ?>


Why not just retrieve the date in that format with the query?
http://dev.mysql.com/doc/refman/5.0/...functions.html

SELECT

DATE_FORMAT('%M %e, %Y',tbl_gigs.fld_gig_Date) as fld_gig_Date,

tbl_gigs.fld_gig_Time, tbl_gigs.fld_gig_Title, tbl_gigs.fld_gig_Place
FROM tbl_gigs WHERE tbl_gigs.fld_gig_Date >= CURDATE()

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jan 5 '06 #6
"John K" <ki*****@yahoo.com> kirjoitti
viestissä:11**********************@f14g2000cwb.goo glegroups.com...
I recently took a job doing PHP but come from an ASP background. I'm
trying to find a conversion equivalent for VBScript's "formatdatetime".

All I want to do is convert a MySQL datetime field into a long date.
e.g. 2006-01-30 to January 1, 2006.

$unixTimeStamp = strtotime('2006-01-30');
date('F d, Y', $unixTimeStamp);

Returns January 30, 2006
--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <an*******************@gmail.com.NOSPAM.invalid>
Jan 5 '06 #7
I tried it didn't produce a result (the date that is). Time produced
result however.

This is what I put in. Did I do something wrong?

<?php
mysql_select_db($database_hthConnect, $hthConnect);
$query_gigRecall = "SELECT DATE_FORMAT('%M %e,
%Y',tbl_gigs.fld_gig_Date) as fld_gig_Date, tbl_gigs.fld_gig_Time,
tbl_gigs.fld_gig_Place FROM tbl_gigs";
$gigRecall = mysql_query($query_gigRecall, $hthConnect) or
die(mysql_error());
$row_gigRecall = mysql_fetch_assoc($gigRecall);
$totalRows_gigRecall = mysql_num_rows($gigRecall);
?>

Jan 5 '06 #8
John K wrote:
I tried it didn't produce a result (the date that is). Time produced
result however.

This is what I put in. Did I do something wrong?

<?php
mysql_select_db($database_hthConnect, $hthConnect);
$query_gigRecall = "SELECT DATE_FORMAT('%M %e,
%Y',tbl_gigs.fld_gig_Date) as fld_gig_Date, tbl_gigs.fld_gig_Time,
tbl_gigs.fld_gig_Place FROM tbl_gigs";
$gigRecall = mysql_query($query_gigRecall, $hthConnect) or
die(mysql_error());
$row_gigRecall = mysql_fetch_assoc($gigRecall);
$totalRows_gigRecall = mysql_num_rows($gigRecall);
?>


maybe it has to do with the name... try this instead:

DATE_FORMAT('%M %e, %Y',tbl_gigs.fld_gig_Date) as formatGigDate

and then:
<?php echo $row_gigRecall['formatGigDate']; ?>

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jan 5 '06 #9
THAT DID IT, THANK YOU!!!! YES!

Jan 5 '06 #10
On Thu, 05 Jan 2006 06:50:25 -0800, John K wrote:
I recently took a job doing PHP but come from an ASP background. I'm
trying to find a conversion equivalent for VBScript's "formatdatetime".

All I want to do is convert a MySQL datetime field into a long date.
e.g. 2006-01-30 to January 1, 2006.
PLEASE provide an example that I can use as a future benchmark so I can

understand the syntax better. I really need to learn PHP and I'm a
little freaked out right now.
Thanks!

That's what PEAR Date is for. Here is the example you provided:
$ php /tmp/ttt.php
January 01, 2006
$ cat /tmp/ttt.php
<?php
require_once("Date.php");
$d=new Date("2006-01-30 00:00:00");
echo $d->format("%B %m, %Y"),"\n";
?>

--
http://www.mgogala.com

Jan 5 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Sune | last post: by
12 posts views Thread by DC Gringo | last post: by
5 posts views Thread by John K | last post: by
2 posts views Thread by fiefie.niles | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.