Converting a date to a day, for example. Mon, Tue, Wed

I have a variable that displays the date in the following format:

201308131830 

What is 2013 - 13 - 18:30

I use this to get the name of the day, but it gets the wrong days:

 $dayname = date('D', strtotime($longdate)); 

Any idea what I'm doing wrong?

+4
source share
5 answers

This is what happens when you save your dates and times in a non-standard format. Working with them becomes problematic.

 $datetime = DateTime::createFromFormat('YmdHi', '201308131830'); echo $datetime->format('D'); 

Look in action

+13
source

Your code works for me

 $date = '15-12-2016'; $nameOfDay = date('D', strtotime($date)); echo $nameOfDay; 

Use l instead of D if you prefer the full textual representation of the name

+6
source

Your code works for me.

 $input = 201308131830; echo date("YMd H:i:s",strtotime($input)) . "\n"; echo date("D", strtotime($input)) . "\n"; 

Output:

 2013-Aug-13 18:30:00 Tue 

However, if you pass 201308131830 as a number, it is 50-100 times larger than a 32-bit integer can represent. [depending on the specific implementation of your system] If your server / PHP version does not support 64-bit integers, the number will overflow and will probably be output as a negative number, and date() will default to Jan 1, 1970 00:00:00 GMT .

Make sure that the source from which you extract this data returns this date as a string, and save it as a string.

+3
source

You cannot use strtotime since your time format is not in the supported PHP date and time formats.

To do this, you need to create a valid date format by first using the createFromFormat function .

 //creating a valid date format $newDate = DateTime::createFromFormat('YmdHi', $longdate); //formating the date as we want $finalDate = $newDate->format('D'); 
+1
source

It looks like you could use date () and the lowercase character "L" as follows:

 $weekday_name = date("l", $timestamp); 

Works well for me, here is the document: http://php.net/manual/en/function.date.php

0
source

Source: https://habr.com/ru/post/1468999/


All Articles