How to add a specific number of days (+1 day) to current date in (ISO & RFC format) for "WordPress"? - wordpress

I have been searching for a long time to find out how easily to add a specific number of days (+1 day) to current date in (ISO & RFC format) for "WordPress"?
Anyway, After tons of testings and failure I came up with the final solution.

Solution:
First of all, We must get the default current post date (get_the_date), Then add the desired format (ISO or RFC), and finally increase it by adding more +days to it as follow:
<?php
$Date = get_the_date('Y-m-d');
echo date('c', strtotime($Date. ' + 10 days'));
?>
The results will be:
- Current date: 2018-09-14
- Increased date: 2018-09-24T00:00:00+00:00 *(with ISO format)*.
This achievement can be useful when you want to validate sitemap, Wordpress feed, or add schema markup to your pages (manually).
Hope this can help you :)

Related

Moment.js doesn't support Microsoft/ISO format?

I'm working on a project where I'm given the Microsoft date/time format, which seems to be exactly like a standard ISO formatting.
When I try to format a date with Moment.js, it doesn't come out right. For example, the format I'm provided with has 'yy' for the 2-digit year and 'd' for a 1-digit day-of-month.
When I look at the Moment.js format documentation I realize the it doesn't even support it? That's a bit odd? Why would it not support a standard?
Or am I doing something wrong? Am I missing something?
I'd really hate to try to write an ISO to Moment.js format converter.
Has anyone had the same problems? If so, how have you resolved this?
Update 2017.10.16 17:32:
After getting a good question from Matt Johnson down in the comment, I read my post again and realized I've been working for so long in this "embedded" web project that I probably wasn't quite clear as to what is meant by "windows giving me a date format". So I wrote the following in response to his question:
#MattJohnson, what I mean is that the web project I'm working on for a client is an IE embedded (OLE) inside a Windows application (compiled binary, not web). The JavaScript portion of the web application "talks" to the desktop application in order to receive/send data. One of the data I receive is a "dateFormat" and "timeFormat" property. The values I receive, I'm told come directly from the Windows machine (based on user configuration of that OS). It happens that years/days are all lowercase, causing Moment.js to not being able to format dates properly. Thus the conversion I now have.
Examples:
Calling
moment().format();
Will produce
"2017-10-13T13:24:47-04:00"
Now, if I want to format it according to the Moment.js documentation, I'd do this:
moment().format('MM/DD/YYYY');
To get this:
"10/13/2017"
The problem is that Windows passes along this format:
"dd/MM/yyyy"
So when I call Moment with it, like:
moment().format('dd/MM/yyyy');
Giving me this:
"Ve/30/yyyy"
While I was expecting this:
13/10/2017
When I look at many other date formatting libraries, I see that they all support the 'yyyy', 'dd' and 'MM' structure. But not Moment.js.
Update 2017.10.13 16:41:
I did a comparison of Microsoft's date/time format to that of Moment.js and saw the differences.
* ---------------------------------------------------------------------------------------------
* Unit Microsoft Examples Moment.js Differnces?
* ---------------------------------------------------------------------------------------------
* day d, dd 1, 01 D, DD Yes, case
* day of week ddd, dddd Fri, Friday ddd, dddd None
* month M, MM, MMM, MMMM 8, 08, Oct., October M, MM, MMM, MMMM None
* year yy, yyyy 17, 2017 YY, YYYY Yes, case
*
* hour h, hh, H, HH 3, 03, 15, 15 h, hh, H, HH None
* minutes m, mm 9, 09 m, mm None
* seconds s, ss 5, 05 s, ss None
*/
Using that information, I quickly wrote a format conversion function:
function windowsFormatToMomentJSFormat(windowsFormat) {
var format = windowsFormat;
console.log("Converting date format...");
if (!windowsFormat) return format;
console.log(" > From : '" + windowsFormat + "'");
format = format.replace(/y/g, 'Y'); // Fix case for years
format = format.replace(/d{4}/g, '#'); // Protect 4-digit DOW sequence
format = format.replace(/d{3}/g, '&'); // Protect 3-digit DOW sequence
format = format.replace(/d/g, 'D'); // Fix case for days
format = format.replace(/#/g, 'dddd'); // Restore our sequence
format = format.replace(/&/g, 'ddd'); // Restore our sequence
console.log(" > To : '" + format + "'");
console.log(" > Applied : '" + moment().format(format) + "'");
return format;
}
It seems to work well, although I wish I was much better at advanced REGEX in order optimize the function and remove the protect/restore code.
So now, my Windows format strings seems to be processed correctly by Moment.js' format() function.
Yes, you will need a conversion function, because date/time formatting tokens are different across various libraries and platforms. There isn't one already developed, that I am aware of.
This is the list of formatting tokens supported in Moment
This is the list of formatting tokens supported in .NET, which is roughly the same as the ones used in the Windows date/time region settings.
Here is a moment plugin that does almost what you want, but it uses Java's token set, not Windows. You could base your implementation on that.
Note that your current function is missing several tokens. In particular the am/pm separator needs translation, and you should think about literals and escape sequences as well.

ASP.NET "String was not recognized as a valid DateTime."

First off, I realize there's a million pages discussing this already. I have looked at least a hundred of them but cannot seem to make this work. My date and time is presented as a string, compiled from javascript to grab client's local time. It is formatted like this: 7/11/2015 8:34 PM.
I currently have:
Dim datetimeformated = DateTime.ParseExact(lblDateTime.Text, "MM/dd/yyyy HH:mm tt", CultureInfo.InvariantCulture)
I have tried many different variants, but this should be correct I think, yet it does not work. Any help is greatly appreciated. TIA
The correct format for your case is: M/dd/yyyy h:mm tt, and perhaps even, M/d/yyyy h:mm tt, if you can have the day of the month as a single digit.
Explanation: Why your format string didn't work.
MM: means that you must always have 2 digits for the month, clearly not the case in your example.
dd: again, means that you must always have 2 digits for the day of the month. Is that the case? Adjust the parameter if needed.
HH: This actually means that you are expecting the hour value as 2-digits using the 24-hour clock (00-23), which is clearly wrong on both accounts. You can have a single digit, and you are not using the 24-hour clock, because you are using the AM/PM designator.
Relevant documentation link: Custom Date and Time Format Strings.

ColdFusion - DateTime Format with GMT offset

I am using ColdFusion 10 to make some REST calls and the date returned is using a GMT offset.
Example: 2013-03-25T14:30:40-04:00
I need this formatted for 2 purposes:
Screen Display so it looks something like mm/dd/yyyy hh:mm:ss
To Insert into mySQL.
I have tried a variety of the CF time/date functions but continue to get the "is not a valid date format"
I thought maybe the #ParseDateTime(i.submitted_at,"pop")# would handle it with POP but same issue.
Spent a few hours now trying multiple variations and googling around now just going in circles. Any ideas would be greatly appreciated.
Thanks!
Have a look at the UDF DateConvertISO8601() on CFLib.
DateConvertISO8601(ISO8601dateString, targetZoneOffset) on CFLib
Description:
This function take a string that holds a date in ISO 8601 and converts it to ODBC datetime, but could be adapted to convert to whatever you like. It also will convert to a datetime in a timezone of your choice by specifying the offset, i.e. it could take a datetime in GMT and convert to PT. See http://www.w3.org/TR/NOTE-datetime for description of ISO 8601, the International Standard for the representation of dates and times.
Return Values:
Returns a datetime.
The source code is viewable at the link I provided.
This, 2013-03-25T14:30:40-04:00, is a string. If you run this:
x = left(2013-03-25T14:30:40-04:00, 19);
you get 2013-03-25T14:30:40. You can use the replace function to replace the T with a space. You can then to this
DateTimeVar =parsedatetime('2013-03-25 14:30:40');
Now you have a datetime variable that you can format. If necessary you can do a datediff with the offset from GMT.
This is an informational answer, not a direct answer to the question.
ColdFusion 11 has updated the ParseDateTime() function so that it will correctly convert the ISO-8601 date/time strings to a ColdFusion datetime object.
With a number of remote requests and responses, the date / time values can often be returned in ISO format. In your case, the mask looks like this:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
In this ISO format, the T string is a literal representation of a marker where the time stamp starts in the string (with the offset following directly).
Below is a reusable function that will convert an ISO date format into a useable ColdFusion date time object:
<cffunction name="ISOToDateTime" access="public" returntype="string" output="false"
hint="Converts an ISO 8601 date/time stamp with optional dashes to a ColdFusion
date/time stamp.">
<cfargument name="Date" type="string" required="true" hint="ISO 8601 date/time stamp." />
<cfreturn ARGUMENTS.Date.ReplaceFirst(
"^.*?(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*$",
"$1-$2-$3 $4"
) />
</cffunction>
You can then call the function like so to output or return a ColdFusion-friendly version of the date time:
ISOToDateTime( "2013-03-25T14:30:40-04:00" )
That function is courtesy of Ben Nadel. The original blog post can be found here:
http://www.bennadel.com/blog/811-Converting-ISO-Date-Time-To-ColdFusion-Date-Time.htm
You can also convert the date time value using the offset, if required. Again, Ben Nadel has a great blog post outlining how to accomplish this:
http://www.bennadel.com/blog/1595-Converting-To-GMT-And-From-GMT-In-ColdFusion-For-Use-With-HTTP-Time-Stamps.htm
CF10 can use this code as stated in the example of the parseDateTime() doc.
<cfset string = "1997-07-16T19:20:30+01:00">
<cfset date = parseDateTime(string, "yyyy-MM-dd'T'HH:mm:ssX")>

drupal7 change format of comment's $created

I have been trying for some time now to reformat the date that appears below the avatar when a comment is made to a blog post.
I have found no way of doing it through any settings, so I have moved over to the template files. Changing the date format of the original blog post was easy, but with comments, no variable holding the date in raw (unix) format is passed in to the template, so I can't reformat and extract the pieces of the date that I want.
The Zen theme help note says:
* - $created: Formatted date and time for when the comment was
created.
I would like to reformat this from
Tue, 2011-09-13 11:57
Into two separate variables,
13 Sep, 2011
11:57
any idéas? Thanks
In your template file you should have access to the $node variable so you can do something like this:
echo date('j M, Y H:i', $node->created);
See the PHP date page for information on the format string used above.

Have wp_get_archives return a string with no year in it

Currently I use wp_get_archives to return the year.
I then call it again with the monthly arg to display the months.
It displays similarly to this, though I am not sure it will work when we are in a different year:
2011
April 2011(1)
March 2011(12)
I want a display like this
2012(when we reach it)
December(8)
November(5)
2011
April(1)
March(12)
Where the number in brackets is the months post count.
Obviously this means stripping out the year from the monthly archives.
Is this possible? I'd rather not modify core wp functions and do this within the theme, I would also rather not rely on a plugin.
Thank you!
While maybe not the best way to do it, give this a try
$string = wp_get_archives('type=monthly&limit=20&echo=0');
$pattern = ' ((19|20)\d{2}(</a>))';
echo preg_replace($pattern, '\\3', $string);

Resources