I can't convert date to format d/m/Y.
date("d-m-Y", strtotime(substr($code, 22, 6)) ); return 21-10-2014
but date("d/m/Y", strtotime(substr($code, 22, 6)) ); return 21/10/2014
How to format?
I would use DateTime to create an object with your format, and then reformat it.
<?php
$date = DateTime::createFromFormat('ymd', '140521');
echo $date->format('d/m/y'); //Output: 21/05/14
https://eval.in/208227
How would you like your formated date be displayed? The d, m and y in "d-m-Y" or "d/m/Y" are responsible for what you wish to display, not - or /
Here is a few list of parameters and their output:
d : Day of the month, 2 digits with leading zeros 01-31
m : Numeric representation of a month, with leading zeros 01-12
Y : A full numeric representation of a year, 4 digits
F : A full textual representation of a month, such as January or March
l : A full textual representation of the day of the week Sunday through Saturday
If you want to display 21 October 2014, you will have to use:
date("d F Y", strtotime(substr($code, 22, 6)) );
$originalDate = "2014-10-21";
$newDate = date("d/m/Y", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
Related
I am currently displaying date in my php code like below
$date = $news_row['date'];
$newDate = date("l M d Y", strtotime($date));
its giving me output like
Monday Mar 08 2021
Instead I want output like
Monday Mar 08th 2021
I have found function
function ordinal($number) {
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number%100) <= 13))
return $number. 'th';
else
return $number. $ends[$number % 10];
}
on this answer
But I am not getting idea how I can use it with my date format. Let me know if anyone here can help me for do it.
Thanks a lot!
$newDate = date('l M jS Y', strtotime($date));
Read about more datetime formats in PHP
https://www.php.net/manual/en/datetime.format.php
l => A full textual representation of the day of the week
M=> A short textual representation of a month, three letters,
j => Day of the month without leading zeros
S => English ordinal suffix for the day of the month, 2 characters
Y => A full numeric representation of a year, 4 digits
You can specify any character that you want:
$newDate = date("l M d\t\h Y", strtotime($date));
But if you want to get 1st, 2nd, 3rd, 4th ... you should use:
$newDate = date("l M jS Y", strtotime($date));
I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>
I have displayed a date in a php page which is in dd-mm-yyyy format like-
$bdate = new DateTime($_SESSION['booking_date']);
$date2=$bdate->format('d-m-Y');
echo $date2;
whic gives output as "23-04-2013".
but i want this like "23rd April 2013". How can i do this in php?
Check out date() formatting strings to understand how DateTime::format() works.
print $bdate->format('jS F Y');
Try this code
$bdate = new DateTime($_SESSION['booking_date']);
$date2=$bdate->format('l jS F Y');
echo $date2;
Do you consider in use Javascript ?
You can use the moment.js
[year, month, day, hour, minute, second]
moment([2010, 1, 14, 15, 25, 50]); // February 14th, 3:25:50 PM
For example... but you can find all in www.momentjs.com
this will work for you
$date2=$bdate->format('jS F Y');
echo $date2;
where
j - Day of the month without leading zeros
S - English ordinal suffix for the day of the month, 2 characters
F - A full textual representation of a month, such as January or March
Y - A full numeric representation of a year, 4 digits
The following code is producing a wrong conversion of a Timestamp (1350553368):
$dateTime = new DateTime();
$dateTime->setTimeStamp(1350553368);
echo $dateTime->format('F n, Y');
PHP converts it to October 10, 2012: http://codepad.viper-7.com/clum0f
However, that timestamp is actually for October 18, 2012: http://www.onlineconversion.com/unix_time.htm
I'm sure it's me, and not PHP, so what am I doing wrong? The code is pretty straightforward, so I can't figure it out.
You are using format 'F n, Y'. n is a numeric representation of the month (October is month 10). Use d (leading zeroes) or j (no leading zeroes). See PHP date() reference.
echo $dateTime->format('F d, Y');
Form PHP DOC
n = Numeric representation of a month, without leading zeros
d = Day of the month, 2 digits with leading zeros
You should replace
$dateTime->format('F n, Y');
With
$dateTime->format('F d, Y');
i was fetching this date from table in the database like this format
Sunday 16th of January 2011 06:55:41 PM
and i want to convert it to be like this format
11-05-2012
how to do that with date function or any function
when i use date function
<td><?php echo date('d-m-Y', $v['v_created']); ?></td>
i get error message
'Severity: Warning
Message: date() expects parameter 2 to be long, string given'
This works for me (just tested on local web server)
<?php
date_default_timezone_set ('Europe/Rome');
$date = "Sunday 16th of January 2011 06:55:41 PM";
//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);
$sql_friendly_date = date('y-m-d H:i', strtotime($date));
echo $sql_friendly_date;
?>
You can format the date as you prefer changing the first parameter of Date function according to: http://it2.php.net/manual/en/function.date.php
You have the following format:
Sunday 16th of January 2011 06:55:41 PM
that is a string based format, so the date information is more or less encoded in a human readable format. Luckily in english language. Let's see, that are multiple things all separated by a space:
Sunday - Weekdayname
16th - Date of Month, numeric, st/nd/th form
of - The string "of".
January - Monthname
2011 - Year, 4 digits
06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
PM - AM/PM
So you could separate each node by space and then analyze the data. All you need is all Monthnames and the sscanf function because you only need to have the month, date of month and year:
$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);
Which will already give you the following variables:
$monthname - string(7) "January"
$day - int(16)
$year - int(2011)
So all left to do is to transpose the monthname to a number which can be done with a map (in the form of an array in PHP) and some formatted output:
$monthnames = array(
'January' => 1,
# ...
);
printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);
So regardless of which problem, as long as the input is somewhat consistently formatted you can pull it apart, process the gained data and do the output according to your needs. That is how it works.
try this. always worked for me
$date = Sunday 16th of January 2011 06:55:41 PM
$new_date = date('d-M-Y', strtotime($date));
echo $new_date;
The format you are using Sunday 16th of January 2011 06:55:41 PM is a wrong format.from the form you are inserted this date in database should be in date(Y-m-d) than the value of date inserted in database like:- 11-05-2012. and you can fetch this and get the format what you want.
<?php
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$new_date = date('d-M-Y', strtotime($old_date));
echo $new_date
?>
more details about date plz visit this url
http://www.php.net/manual/en/datetime.createfromformat.php