-1

I'm using DB(sql) to store date. while displaying in php page date format is showing incorrectly and I want to change it to DD-MM-YYYY fromat Currently it is showing as "02/07-14"

DB:

last_download_date (Name) -> datetime (Type)

Code in website

<tr>
    <td>Last Downloaded Date</td>
    <td>{if $last_download_date eq ""}Not downloaded yet.{else}{$last_download_date}{/if}</td>
</tr>
Phil
  • 141,914
  • 21
  • 225
  • 223
Uday
  • 9
  • 1
  • 1
    I would normally suggest you look at your database vendor's date functions to figure out how to format the internal SQL `datetime` type into something useful in PHP, but you failed to mention which specific DB solution you're using. – TML Oct 09 '14 at 04:54
  • What templating language is that? Most templating engines have date formatting capabilities. Please show how you're setting `$last_download_date`, otherwise, we're all just blindly guessing – Phil Oct 09 '14 at 04:55
  • possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Bud Damyanov Oct 09 '14 at 06:50

2 Answers2

1

Use DateTime::createFromFormat:

$date = DateTime::createFromFormat('d/m-Y', $date_from_mysql);
$the_date = $date->format('d-m-Y');
rogelio
  • 1,521
  • 14
  • 19
  • 1
    We don't know if it's `d/m-Y` or `m/d-Y` – Phil Oct 09 '14 at 04:59
  • @Phil, yes, but I think it doesn't matter at this point. what matters is the consistency. Example: From `02/07-2014` to `02-07-2014` is correct. The programmer must know which corresponds each. – rogelio Oct 09 '14 at 05:08
  • Fair enough, have an upvote. I still think the problem is in how OP is retrieving the value in the first place. – Phil Oct 09 '14 at 05:09
  • Me too, maybe mysql `date_format` is enough. Or equivalent for another DB. – rogelio Oct 09 '14 at 05:14
  • I wrote "or equivalent for another DB" :P – rogelio Oct 09 '14 at 05:15
-1

You can take a look at the documentation for PHP date function:

http://php.net/manual/en/function.date.php

You could use

$formated_date = date("d-m-Y", strtotime($dbdatestring));
Yoel Nunez
  • 2,096
  • 1
  • 11
  • 18
  • 1
    Note that the value he's getting back from the database does not appear to be a valid epoch timestamp, so this may not be the most helpful answer ever. – TML Oct 09 '14 at 04:57