Modify DateTime on Month short name formatted date string - php

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.

Related

date conversion in CodeIgniter / PHP does not gives output

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));
?>

PHP: Convert date to Javascript date (January = zero)

I'm using Highcharts and need the dates formatted like this:
UTC.(2016,11,01) // for December 1st 2016
Is there an easy way to do this? Because
date('Y-m-d', strtotime('-1 month'))
gives me the wrong output for January.
You don't need sub the month. Just use other format as ATOM/RFC3339. It's fully supported by javascript Date class
$date = new DateTime('2016-01-01');
echo $date->format(DateTime::ATOM);
Result:
2016-01-01T00:00:00-05:00
Javascript:
var date = new Date('2016-01-01T00:00:00-05:00');
console.log(date)
As I'm live in Brazil, my browser show as
Fri Jan 01 2016 03:00:00 GMT-0200 (Horário brasileiro de verão)
If you have any other format as input, you could use DateTime::createFromFormat and the rest still the same
$date = DateTime::createFromFormat('m/d/Y' , '12/01/2016');

Format the formatted date for mysql

How can I format this date 01 August, 15 for mysql DATE field. So I need this 2015-08-01 format from 01 August, 15 this format in PHP. Tried following but not work
echo date('Y-m-d',strtotime('01 August, 15'));
It is because strtotime() does not understand what 01 August, 15 means. Try it:
var_dump(strtotime('01 August, 15')); // false
The 15 at the end is too ambiguous; it could be the day of the month or a short year.
The easiest way to make this work is probably to use DateTime::createFromFormat, like so:
$date = '01 August, 15';
$parsed = DateTime::createFromFormat('d F, y', $date);
echo $parsed->format('Y-m-d');
If you control the format of the date then you could also make it easier to parse. Formatting it like 01 August 2015 would work, for example.
First remove the , out of the date and then use the strtotime function.
So:
$date = "01 August, 15";
$date = str_replace(",", "", $date);
echo date("Y-m-d",strtotime($date));

How convert date format in php

hello guys i have date like this
25 March 2014 - 16:45
i want to convert this date in to this format how to do that please Help
2014-03-25 16:45:00
i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45'));
but it's not work
i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45')); but it's not work
The reason your code doesn't work is because '25 March 2014 - 16:45' is not in a format that strtotime() can parse.
strtottime() is good at handling a wide range of formats, but it can't cope with absolutely anything; there's just too much variation possible.
I suggest that you try PHP's DateTime class instead. This class has a method called createFromFormat() which allows you to specify the format of the incoming date string. This makes it easier for it to parse, and allows for formats that might not be recognised otherwise, like yours.
Try this:
$date = DateTime::createFromFormat('j F Y - H:i', '25 March 2014 - 16:45');
echo $date->format('Y-m-d H:i:s');
Use PHP DateTime
You have "-" in "25 March 2014 - 16:45" in the string so it not be able to read by DateTime
So work around would be
$str = "25 March 2014 -16:45";
$date = new DateTime(str_replace("-","",$str));
echo $date->format('Y-m-d H:i:s');
I've successfully tried the following with your string:
$a = strptime('25 March 2014 - 16:45', '%d %B %Y - %R');
$time = mktime(
$a["tm_hour"],
$a["tm_min"],
$a["tm_sec"],
$a["tm_mon"]+1,
$a["tm_mday"],
$a["tm_year"]+1900
);
$converted = date('Y-m-d H:i:s',$time);
Try this:
$creation = date('Y-m-d H:i:s',strtotime('25 March 2014 16:45'));

strtotime - a second before midnight

I attempted this:
$date_string = strtotime('6 Mar, 2011 23:59:59');
But I think PHP can't interpret that for some reason as it returned empty. I tried this:
$date_string = strtotime('6 Mar, 2011 midnight');
The above worked but I need it to be a second before midnight i.e. the last second of the day. How can I get strtotime to return this without changing the 6 Mar, 2011 part?
Hope this helps. I used this and it gives todays timestamp just before midnight. Counter intuitive.
$today_timestamp = strtotime('tomorrow - 1 second');
It works for me if I use March 6, 2011 23:59:59. Any chance of changing the input format?
Other than that, you could of course subtract 1 second from the timestamp. Note however that you need to use March 7:
$date_string = strtotime('7 Mar, 2011 midnight') - 1;
Why not use mktime?
mktime(23,59,59,3,6,2011);
If you're on PHP 5.3 or greater, you could use the DateTime class.
The createFromFormat function allows you to manually specify how to parse your input date string.
$date = '6 Mar, 2011 23:59:59';
$timestamp = DateTime::createFromFormat('d M, Y H:i:s', $date)->getTimestamp();

Categories