[Help] SQL/PHP Date [Help]

Status
Not open for further replies.

Ink

Member
Jun 4, 2010
61
11
Okay I've done the query and it successfully brings out "2012-05-18" which is todays date. I don't know how to make it so that the date is displayed as UK represent it I want it to read "18-05-2012". Anyone know how to display the date the other way?

Thanks in advanced if anyone knows how to do this.
 

Ink

Member
Jun 4, 2010
61
11
I'm not too sure. I was entering it through the database and the "date" option allows you to click a calendar but displays the date as 2012-05-18 :c
 

tyr0ne

Active Member
Mar 29, 2012
141
14
That's what I asked how to do?...
What iNerd meant, I think is that normally you can specify the order.

For example

PHP:
echo date("Y/m/d")

that will give you something like 2012/05/20

So what you're gonna do is use this:

PHP:
<?php
$newDate = date('d/m/Y', strtotime('$date'));
?>
Where $date is the date in your MySQL (ie: 18-05-2012)
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
When entering the date into the database, don't format the date first; insert the timestamp into the database, for example:

PHP:
<?php
$time = time();
mysql_query( "INSERT INTO `table` (`field`, `time`) VALUES ('value', '" . $time . "')" );
?>

Then when you retrieve the data from the database, you can format it however you like:

PHP:
<?php
$query = mysql_query( "SELECT * FROM `table` WHERE `field` = 'value'" );
if ( (int) mysql_num_rows( $query ) !== 0 )
{
    $row = mysql_fetch_array( $query );
    echo 'American: ' . date( "m/d/y", $row["time"] ) . '<br />';
    echo 'British: ' . date( "d/m/y/", $row["time"] ) . '<br />';
    echo 'Day and time: ' . date( "l, g:i a", $row["time"] );
}
else
{
    echo 'Record does not exist';
}
?>

So essentially, you can use the same 'date' over and over again using the same data without having to insert multiple date formats into the database.

I hope this helped, I'm not very good at explaining things lol. Also, not sure if the code will work but it looks ok to me.

Good luck.
 
  • Like
Reactions: Ink

Ink

Member
Jun 4, 2010
61
11
When entering the date into the database, don't format the date first; insert the timestamp into the database, for example:

PHP:
<?php
$time = time();
mysql_query( "INSERT INTO `table` (`field`, `time`) VALUES ('value', '" . $time . "')" );
?>

Then when you retrieve the data from the database, you can format it however you like:

PHP:
<?php
$query = mysql_query( "SELECT * FROM `table` WHERE `field` = 'value'" );
if ( (int) mysql_num_rows( $query ) !== 0 )
{
    $row = mysql_fetch_array( $query );
    echo 'American: ' . date( "m/d/y", $row["time"] ) . '<br />';
    echo 'British: ' . date( "d/m/y/", $row["time"] ) . '<br />';
    echo 'Day and time: ' . date( "l, g:i a", $row["time"] );
}
else
{
    echo 'Record does not exist';
}
?>

So essentially, you can use the same 'date' over and over again using the same data without having to insert multiple date formats into the database.

I hope this helped, I'm not very good at explaining things lol. Also, not sure if the code will work but it looks ok to me.

Good luck.


Thanks Mark, Exactly what I needed.
 
Status
Not open for further replies.

Users who are viewing this thread

Top