I have a variable that outputs the date in the following format:
201308131830
Which is 2013 - Aug - 13 - 18:30
I am using this to retrieve the day name but it is getting the wrong days:
$dayname = date('D', strtotime($longdate));
Any idea what I am doing wrong?
This question is related to
php
Very Simply Short week day name with Month and year
echo date('D, d-M-y');
output
Tue, 16-Feb-21
as per my requirements
return $item->start_time->format("D, d-M");
output
Tue, 16-Feb
Your code works for me.
$input = 201308131830;
echo date("Y-M-d 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 to 100x larger than can be represented by a 32-bit integer. [dependent on your system's specific implementation] If your server/PHP version does not support 64-bit integers then the number will overflow and probably end up being output as a negative number and date()
will default to Jan 1, 1970 00:00:00 GMT
.
Make sure whatever source you are retrieving this data from returns that date as a string, and keep it as a string.
$date = new \DateTime("now", new \DateTimeZone('Asia/Calcutta') );
$day = $date->format('D');
$weekendnaame = weekedName();
$weekid =$weekendnaame[$day];
$dayname = 0;
$weekiarray = weekendArray($weekid);
foreach ($weekiarray as $key => $value) {
if (in_array($value, $request->get('week_id')))
{
$dayname = $key+1;
break;
}
}
weeknDate($dayname),
function weeked(){
$week = array("1"=>"Sunday", "2"=>"Monday", "3"=>"Tuesday", "4"=>"Wednesday", "5"=>"Thursday", "6"=>"Friday", "7"=>"Saturday");
return $week;
}
function weekendArray($day){
$favcolor = $day;
switch ($favcolor) {
case 1:
$array = array(2,3,4,5,6,7,1);
break;
case 2:
$array = array(3,4,5,6,7,1,2);
break;
case 3:
$array = array(4,5,6,7,1,2,3);
break;
case 4:
$array = array(5,6,7,1,2,3,4);
break;
case 5:
$array = array(6,7,1,2,3,4,5);
break;
case 6:
$array = array(7,1,2,3,4,5,6);
break;
case 7:
$array = array(1,2,3,4,5,6,7);
break;
default:
$array = array(1,2,3,4,5,6,7);
}
return $array;
}
function weekedName(){
$week = array("Sun"=>0,"Mun"=>1,"Tue"=>3,"Wed"=>4,"Thu"=>5,"Fri"=>6,"Sat"=>7);
return $week;
}
Seems like you could use date() and the lowercase "L" format character in the following way:
$weekday_name = date("l", $timestamp);
Works well for me, here is the doc: http://php.net/manual/en/function.date.php
You can not use strtotime
as your time format is not within the supported date and time formats of PHP.
Therefor, you have to create a valid date format first making use of createFromFormat function.
//creating a valid date format
$newDate = DateTime::createFromFormat('YmdHi', $longdate);
//formating the date as we want
$finalDate = $newDate->format('D');
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
echo date('D', strtotime($date));
echo date('l', strtotime($date));
Result
Tue
Tuesday
For other language use day of week to recognize day name
For example for Persian use below code
$dayName = getDayName(date('w', strtotime('2019-11-14')));
function getDayName($dayOfWeek) {
switch ($dayOfWeek){
case 6:
return '????';
case 0:
return '?? ????';
case 1:
return '?? ????';
case 2:
return '?? ????';
case 3:
return '???? ????';
case 4:
return '??? ????';
case 5:
return '????';
default:
return '';
}
}
More info : https://www.php.net/manual/en/function.date.php
Source: Stackoverflow.com