Как преобразовать mysql дату и время в PHP к формату m/d/y?

clientWidth и clientHeight является свойствами DOM, которые показывают текущий размер в браузере внутренних размеров элемента DOM (исключая поле и границу). Таким образом в случае элемента IMG, это получит фактические размеры видимого изображения.

var img = document.getElementById('imageid'); 
//or however you get a handle to the IMG
var width = img.clientWidth;
var height = img.clientHeight;

11
задан JasonDavis 28 August 2009 в 03:13
поделиться

6 ответов

I think what you really want is this:

$fromMYSQL = '2007-10-17 21:46:59';
echo date("m/d/Y", strtotime($fromMYSQL));

The second argument of date is a timestamp and I think what is happening is PHP sees your string as a -1 timestamp... thus 12/31/1969.

So, to get a timestamp from the string version of the date, you use strtotime

42
ответ дан 3 December 2019 в 00:49
поделиться

SQL:

SELECT whatever, UNIX_TIMESTAMP(date) date FROM table WHERE whatever

PHP:

date('m/d/Y', $result['date']);
7
ответ дан 3 December 2019 в 00:49
поделиться

You need a capital Y int the date format string. the lowercase y gives a two digit year and the upper case 'Y' give a four digit year.

$fromMYSQL = '2007-10-17 21:46:59';
echo date("m/d/Y", strtotime($fromMYSQL));

PHP Manual Page For date may be of some assistance.

2
ответ дан 3 December 2019 в 00:49
поделиться

I covered this in my answer to http://stackoverflow.com/questions/499014/i-need-to-change-the-date-format-using-php/499021#499021 - basically, date() expects a Unix timestamp from which it calculates the date/time and formats that. You're passing in a string which comes from the MySQL query result, which probably gets mangled by PHP's duck typing into something that looks like a Unix timestamp, but makes no sense.

I explain a couple of approaches to dealing with date columns in my answer.

2
ответ дан 3 December 2019 в 00:49
поделиться

Two issues:

1) You need a capital Y

2) You need a correct date type.

Try

$fromMYSQL = date_create  ('2007-10-17 21:46:59');    
echo date("m/d/Y", $fromMYSQL);

Oops, strtotime is the right conversion to a timestamp. date_create returns a DateTime object.

1
ответ дан 3 December 2019 в 00:49
поделиться

The best way is to convert the dateformat direct in your Querystring:

$TimeFormat = "%m/%d/%Y";  // your pref. Format

$sql = "SELECT DATE_FORMAT(DateCol , '" . $TimeFormat . "') as ConvertDate FROM tblTest";

Otherwise you can modify this function to your needs:

function format_date($original, $format) {
    if (empty($original)) {
        $original = date("Y-m-d H:i:s");
    }
    $original = ereg_replace("30 Dez 1899", "30-01-1973", $original);
    $format = ($format=='date' ? "%m-%d-%Y" : $format);
    $format = ($format=='germandate' ? "%d.%m.%y" : $format);
    $format = ($format=='germandaydate' ? "%A, %d.%m.%Y" : $format);
    $format = ($format=='germantime' ? "%H:%M" : $format);
    $format = ($format=='germandatetime' ? "%d.%m.%y %H:%M:%S" : $format);
    $format = ($format=='datetime' ? "%m-%d-%Y %H:%M:%S" : $format);
    $format = ($format=='mysql-date' ? "%Y-%m-%d" : $format);
    $format = ($format=='mysql-datetime' ? "%Y-%m-%d %H:%M:%S" : $format);
    $format = ($format=='mssql-date' ? "%Y%m%d" : $format);
    $format = ($format=='mssql-datetime' ? "%Y%m%d %H:%M:%S" : $format);
    $format = ($format=='Ymd' ? "%Y-%m-%d" : $format);
    return !empty($original) ? strftime($format, strtotime($original)) : "";
}
3
ответ дан 3 December 2019 в 00:49
поделиться
Другие вопросы по тегам:

Похожие вопросы: