DateTime format full month not as documented - php

Trying to output the full month name using DateTime object but somehow I'm not interpet documentation show here .
I want the date out put a date like 18 November 2012 but Im not sucseeding
$date = new DateTime();
$datum = $date->format("d m Y");
outputs 18 11 2012 ??
reading on this format dd ([ \t.-])* m ([ \t.-])* y should output correct but this does not work at all ??

For the formatting characters, see the date() manual page. This is referred to several times on the DateTime::format() manual page.
For the full month name you want to use the formatting character F.
A full textual representation of a month, such as January or March
The "Date Formats" manual page that you linked to is for the input date string, not the output format.

Related

Convert a string to correct date format (joomla format) [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I got a barebone joomla version (so just pure php using the joomla cms and table names). And the published date of an article is saved like this:
2016-04-06 14:38:52
How can I convert that string into something like:
6 April 2016
So ignore the time and only use the date in the correct format.
How can this be done using PHP (no joomla build in functions)?
You can do it with date and strtotime function of php
$t="2016-04-06 14:38:52";
echo date('j F Y', strtotime($t));
F :- A full textual representation of a month, such as January or March
j :- Day of the month without leading zeros
Y :- A full numeric representation of a year, 4 digits
OUTPUT
6 April 2016
You can do it also in object oriented way:
//create a DateTimeObject
$dateTime = new DateTime($date);
echo $dateTime->format('j F Y'); //output: 6 April 2016
Explanation of j F Y:
j - Day of the month without leading zeros
F - A full textual representation of a month, such as January or March
Y - A full numeric representation of a year, 4 digits
We can also do this using the DateTime class native to PHP.
$date = "2016-04-05 16:00:00";
$odate = DateTime:createFromFormat('Y-m-d H:i:s', $date);
Note that I use the createFromFormat method to initiate the DateTime object. This will prevent dates such as 2016-04-05 from being interpreted in the wrong way. This date could just as well be The 4th of May.
Now, to output
// Output
echo $oDate->format('j F Y');
F - A full textual representation of a month, such as January or March
j - Day of the month without leading zeros
Y - A full numeric representation of a year, 4 digits

How to change format of date from that stored in db [php]?

I am storing date time in database as $date=date("Y-m-d H:i:s"); Now on most places I use it in the same format as it is stored 2016-03-01 19:04:18, but on one place I would rather prefer output to be March 01 at 19:04 how can I change format of output from database from 2016-03-01 19:04:18 to March 01 at 19:04.
Working with dates in PHP
First of all we will need to retrieve the date from the database. You can do this however you please.
What we are going to do next is to pass your date to a PHP DateTime object. This will give us easy options to modify the format to our liking.
// $dbDate is the date you got from your database
$date = new DateTime($dbDate);
// If we want to make sure PHP uses the correct date format,
// We can also use createFromFormat
$date = DateTime::createFromFormat('Y-m-d H:i:s', $dbDate);
Now that we have this object we are going to output it in the format you want it to be.
echo $date->format('M d').' at '.$date->format('H:i');
// M - Textual representation of the month ex. January, February, March
// d - Day of month in numeric value ex 1, 14, 27
// H - 24-hours representation of the hour ex 17, 23, 08
// i - Minutes ex 23, 45, 58
For more parameters and information:
PHP.net - DateTime()
Format it like this
date('d-M-Y H:i');
instead of
date("Y-m-d H:i:s");
Play around with these d-M-Y H:i to get the desired result
parse the date variable to it to tell it which variable to format
date("Y-m-d H:i:s", $date);
also see documentation here on date function in PHP
http://php.net/manual/en/function.date.php

Issue with date_create_from_format()

I am using a plugin to create wordpress posts from a twitter feed, and I am trying to edit it so the published time is the same as the tweeted time, rather than the time the cron was run.
Unfortunately, twitter's API returns an already formatted date string, instead of a timestamp, so I am having to parse it and then save it in a wordpress friendly format.
// Wed Jun 06 20:07:10 +0000 2012 (Twitter formatted date example)
// 2014-03-10 18:30:26 (Wordpress formatted date example)
$tweet_date = $tweet->created_at;
$tweet_date = date_create_from_format("D M d h:i:s O Y", $tweet_date);
$tweet_date = date("Y-m-d h:i:s", $tweet_date);
Unfortunately, all I am getting from this the Unix Epoch (Jan 1st, 1970).
I know I must be missing a step, but I can't figure out where.
You had two issues:
1) You were using h for hours when you meant H for 24 hour periods
2) You need to use date_format() when using date_create_from_format() as that function returns a DateTime object which is not compatible with date()
$tweet_date = date_create_from_format("D M d H:i:s O Y", 'Wed Jun 06 20:07:10 +0000 2012');
echo date_format($tweet_date, 'Y-m-d H:i:s');
See it in action
The problem is because you're mixing and matching between PHP's old and new-style date handling.
date_create_from_format() is part of the newer API, and outputs a DateTime object, not the timestamp integer that the older date() function is expecting.
Ideally you should stick entirely with either the new or the old date functions. You can switch between them, but there usually isn't a need to.
For example, in your case, the DateTime object generated by date_create_from_format() has a perfectly usable format() method attached to it, which does exactly the same as the date() function, but on a DateTime object.
$tweet_date_object = date_create_from_format("D M d h:i:s O Y", $tweet_date);
$tweet_date = $tweet_date_object->format("Y-m-d h:i:s");

PHP: Convert "13 September 2013 - 23:55" to SQL Server formatted datetime format

Trying to get my head around this, but can't seem to figure it out. I'm using PHP and attempting to convert the user-submitted date/time selection which outputs:
13 September 2013 - 23:55
I would like to convert that to the standard SQL Server format like:
2013-09-13 23:55:00.000
I've messed with the PHP strtotime() function sending it only the "13 September 2013" part, but it only outputs a long (seemingly) random number.
Is there any easier method for this?
Have a go with:
$date = DateTime::createFromFormat('d F Y - H:i','13 September 2013 - 23:55');
echo $date->format('Y-m-d H:i:s');
This lets you specify a format to read from.
strtotime returns the unix timestamp, you need to turn it to date string.
php > echo date('Y-m-d H:i:s', strtotime('13 September 2013 23:55'));
2013-09-13 23:55:00
You have to make two separate functions,
for converting month to numeric
year to two digit number
and after that you can break the user input to its desired three parts.

Convert a date in php, toString function isn't working

I'm trying to convert a date to a string by using the toString('dd MMMM YYY') function.
But here, this isn't working.
Here is my code:
$date_start = new Zend_Date(strtotime($this->startdate));
echo($date_start);
The result is: 31 Dec 2012 00:00:00
$date_input = $date_start->toString('dd MMMM YYY');
echo($date_input);
The result is: 31 December 2013
What do I have to do to obtain 31 December 2012 ?
This is a known issue described here: http://framework.zend.com/issues/browse/ZF-5297
Note that the default ISO format differs from PHP's format which can be irritating if you have not used in previous. Especially the format specifiers for Year and Minute are often not used in the intended way.
For year there are two specifiers available which are often mistaken. The Y specifier for the ISO year and the y specifier for the real year. The difference is small but significant. Y calculates the ISO year, which is often used for calendar formats. See for example the 31. December 2007. The real year is 2007, but it is the first day of the first week in the week 1 of the year 2008. So, if you are using 'dd.MM.yyyy' you will get '31.December.2007' but if you use 'dd.MM.YYYY' you will get '31.December.2008'. As you see this is no bug but a expected behaviour depending on the used specifiers.
Use "yyy" instead of "YYY".
Use yyy instead of YYY.
YYY is the ISO-8601 date, which is different from a calendar date.
http://framework.zend.com/manual/1.12/en/zend.date.constants.html

Categories