D
D
dk-web2015-08-19 15:07:37
PHP
dk-web, 2015-08-19 15:07:37

How to correctly format the date in php?

Confused with dates - a hell of a mixture of formats ... php, moment.js datetimepickera and entering into the database ... I
form on the client - jquery and moment.js

formatD_T = 'D MMMM GGGG в HH:mm'; // выводит 3 августа 2015 в 13:00
или 
formatD_T= formatD ="D MMMM GGGG"; // выводит 3 августа 2015
var newDate =  moment(date).format(formatD_T);

then it's all serialized and sent to the handler...
used to do just -
$data['event_start']=date("Y-m-d H:i:s", strtotime($data['event_start']));

now, of course, does not understand this format and sends to 1970 ...
decided through new DateTime and also stuck (((
$datetime = new DateTime($data['event_start']);
  $datetime->createFromFormat('d M Y', $data['event_start']); // формат вроде верный, по документации смотрел
  print_r ($datetime);

Answer...
3 августа 2015string<br />
<b>Fatal error</b>:  Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (3 августа 2015) at position 0 (3): Unexpected character' in C:\xampp\htdocs\ac_new\admin\mod_calendar\handler_calendar_new1508.php:25
Stack trace:

Tell me, pliz, how to correctly convert the string on August 3, 2015 into a format understandable for MySQL?
UPDATE:
$date = DateTime::createFromFormat('d M Y', '15 February 2009');
echo $date->format('Y-m-d');

works, but the Russian language (August) does not perceive ... that's the ambush

Answer the question

In order to leave comments, you need to log in

5 answer(s)
O
Oleg Krasavin, 2015-08-19
@dk-web

If the deadline is yesterday:

$months = ['April' => 'Апреля',  'June' => 'Июня', ... ];

$date_string = '15 Апреля 2009';
$date_string = str_ireplace(
    array_values($months), 
    array_keys($months), 
    $date_string
);

$date = DateTime::createFromFormat('d M Y', $date_string);
echo $date->format('Y-m-d');

PS php 5.4+

D
Dmitry Kravchenko, 2015-08-19
@mydearfriend

Use timestamp

E
evnuh, 2015-08-19
@evnuh

To stop irritating yourself, just store and send the date everywhere in unix timestamp, and format it already at the output.

O
OnYourLips, 2015-08-19
@OnYourLips

You must use the ISO 8601 format
In PHP, this is done via $datetime->format('c'); or $datetime->format(DateTime::ISO8601)

A
AlikDex, 2015-08-20
@AlikDex

why convert ships there? Use any datetimepicker with a user-friendly interface. Like this:
https://eonasdan.github.io/bootstrap-datetimepicker/
what difference does it make to the user what will be written in the input field.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question