I am trying to change the date 13 December, 2016 using date('Y-m-d',strtotime('13 December, 2016')) and it gives me the result of 2017-12-13, what i am missing here
Actually that comma within the date strings create an issue while using the strtotime function you can replace it with str_replace or instead you can simply use DateTime::createFromFormat method so no need of using extra function like as
$date = DateTime::createFromFormat('d F, Y','13 December, 2016');
echo $date->format('Y-m-d');
or simply date_create_from_format
$date = date_create_from_format('d F, Y','13 December, 2016');
echo $date->format('Y-m-d');
date('Y-m-d', strtotime('13 December 2016'));
You need to remove your comma.
This will give the expected result.
Try this
$date = '13 December 2016';
echo date('d-m-Y', strtotime($date));
The m formats the month to its numerical representation there.
Mistake: You don't have to put coma there.
use
$time = strtotime('10/21/2016');
$newformat = date('Y-m-d',$time);
echo $newformat;
Related
I'm entering this date :
$user_entered_date = '30 November, 2020';
when i am change date format using this command
$new_user_entered_date = date( 'Y-m-d', strtotime($user_entered_date) );
then result is :
2018-11-30
Tell me what is the solution of this problem or where i'm wrong.
Your mistake is the comma ', ' you have to it without like this:
$user_entered_date = '30 November 2020';
You need to use createFromFormat if you have no control over the input format.
$user_entered_date = DateTime::createFromFormat('d M, Y', '30 November, 2020');
$new_user_entered_date = date_format($user_entered_date, 'Y-m-d');
echo $new_user_entered_date;
With the comma,it ignores the 2020 year and takes 30 November only
The date formed is hence in 2018,use without comma for your answer
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 want to display my date as day/month/year.
example 31st October 2011
echo '<h4>Date Of Birth: '.$row['dob'].'</br></h4>';
You can try below code ::-
echo '<h4>Date Of Birth: '.date('dS F Y', strtotime($row['dob'])).'</br></h4>';
// ^^^^^^^ Here the is format of date, in which format you want.
Output will be ::-
31st October 2011
For more you can refer click here
<?php
$mydate = "2010-03-3";
$newDate = date("d M Y", strtotime($mydate));
$new_date = date('dS F Y', strtotime($newDate));
echo $new_date;
?>
/*Out Put*/
03rd March 2010
So if you have that data stored as or you can convert it to 3 variables representing DD MM YYYY then I'd suggest creating array of months and an array of post-fixes(don't know how to call it, but in your example it's 31st) and getting DD % 10 to use reminder for post-fix array
$month = ['January','February',....,'December']; //remember it's $month[MM-1];
$post = ['th','st','nd',...,'th'] //not sure if this on is correct but hope you get the idea
echo date('dS F Y', strtotime($row['dob']));
Refer to DateTime. I normally avoid strtotime() conversions.
Heres a working solution:
<?php
$date = '2016-11-12';
$newDate = new DateTime($date);
echo $newDate->format('jS F Y');
?>
OUTPUT:
12th November 2016
I'd like to use the datetime->modify function on a date string that's formatted like "21 Jan 2016". When I use the datetime->modify and add 1 day, it gives me a result of 30 Apr 2017. I know that if I don't use the short month name and use a number instead (i.e. 01), it will work fine but I would like to get it work this way with short month name. Is this possible?
Please see code below:
<?php
$date = "21 Jan 2016"; // this is my date string
$newdate = new DateTime($date );
$date2 = $newdate->modify('+1 day'); // add 1 day to date string
echo $date2->format("d-M-Y");
?>
RESULT is:
30-Apr-2017
RESULT WANTED
22-Jan-2016
The problem is that you are trying to create a DateTime object from a non-ISO format. That's that part that is not working.
Take a look at: http://php.net/manual/ro/datetime.createfromformat.php
You will need to have something like
DateTime::createFromFormat('d M Y', '21 Jan 2016');
Full example:
$tomorrow = DateTime::createFromFormat('d M Y', '21 Jan 2016')->modify('+1 day')->format("d-M-Y");
echo($tomorrow);
The format of the $date variable is incorrect. Off the top of my head, there are two easy ways to fix this:
Set $date = "Jan 21, 2016"
Set $date = "21-Jan 2016"
More options: https://secure.php.net/manual/en/datetime.formats.date.php
Your date format was wrong. That's all.
I have a string which is in the format: dd/mm/yy
e.g. 29/03/14
But when I print it using date() I get a completely different date!
What am I missing?
$endDate = "29/03/14";
echo date("jS F, Y", strtotime( $endDate ));
1st January, 1970
I even tried:
echo date("jS F, Y", strtotime( trim($endDate) ));
With no luck!
I am reading $endDate from a text file
What I am trying to do is find out if it is the last day of the month...
i.e. Current month is 03 - March has 31 days
The day of month in file is 29
This is not the last day of the month
Your date format is not valid.
Check the Date Formats
You might consider using date_create_from_format('d/m/y','29/03/14'); and work with the DateTime object.
Use DateTime::createFromFormat
$date = DateTime::createFromFormat('d/m/y', '29/03/14');
echo $date->format("jS F, Y");
For getting the last day
echo $date->format('t');
Your date format isn't recognized by strtotime(). Use DateTime class instead:
$dateObj = DateTime::createFromFormat('d/m/y', $str);
echo $dateObj->format('jS F, Y');
Output:
29th March, 2014
Demo
What I am trying to do is find out if it is the last day of the month...
For that, you just have to check if the given date is the same as the last day of the month (which can be obtained with t format character)
$str = '29/03/14';
$dateObj = DateTime::createFromFormat('d/m/y', $str);
if ($dateObj->format('d') == $dateObj->format('t')) {
echo 'Given date is the last day of the month';
}