Jump to content

[SOLVED] Outputting date - very simple!


garry

Recommended Posts

So I store the date something was created in my database and it stores it like "0000-00-00 00:00:00". What I want to do is get this from the database and change it into "Wednesday 12th August" - that sorta format. Can anyone help?

Link to comment
Share on other sites

I take it you're using a unix timestamp to get that date format, i.e. using the timestamp data type in SQL.

 

You can just use

 

<?php
$date = date('l jS F Y'); // Wednesday 12th August 2008
mysql_query("INSERT INTO `db_name` . `table_name` ( `field_name` ) VALUES ( '$date' )")
  or die('MySQL Error: ' . mysql_error());
?>

 

Check out http://uk2.php.net/manual/en/function.date.php

Link to comment
Share on other sites

Ah yes, I am using a unix time stamp. I'm using the now() function to record the date and time. The only problem is that I already have a bunch of dates recorded in the database using that previously mentioned format, so how can I convert it to the new format from that?

Link to comment
Share on other sites

<?php
error_reporting(E_ALL);
$sql = mysql_query("SELECT `date` FROM `db_name` . `table_name`") or die('MySQL Error: ' . mysql_error());
while($row = mysql_fetch_array($sql)) {
  $exp = explode(' ', $row['date']); // Return the date only without the time.
  $date = date('l jS F Y', $exp[0]);
  print $date;
  // OR
  mysql_query("UPDATE `db_name` . `table_name` SET `date` = '$date'");
}
?>

 

That should work, however don't hold me to it.

Link to comment
Share on other sites

Hmm, the code you said didn't quite work

 

The date it is getting from the database is "2008-06-06 23:02:58"

 

When I use date('l jS F Y', $date)

 

I get the following error:

 

Notice: A non well formed numeric value encountered in /htdocs/dev/musicnews.php on line 103

 

Note - line 103 is the date() function

Link to comment
Share on other sites

<?php
error_reporting(E_ALL);
$sql = mysql_query("SELECT `date` FROM `db_name` . `table_name`") or die('MySQL Error: ' . mysql_error());
while($row = mysql_fetch_array($sql)) {
   // $exp = explode(' ', $row['date']); // Return the date only without the time.
  $date = date('l jS F Y', $row['date']);
  print $date;
  // OR
  mysql_query("UPDATE `db_name` . `table_name` SET `date` = '$date'");
}
?>

 

Try that, sorry if i'm not being much help.. I'm at work so it's difficult for me to go in-depth.

Link to comment
Share on other sites

Hmm, nope, still doesn't work.

 

All I'm trying to do is turn this: "2008-06-06 23:36:41", into something like this "Friday 6th June 2008"

 

I tried using that $date = date('l jS F Y', $row['date']); function but it just returns the same error:

 

Notice: A non well formed numeric value encountered in /htdocs/dev/musicnews.php on line 103

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.