Problem/Motivation

There are some date formats that can not be easily resolved with strtotime().

It'd be useful if the user could specify the custom format the date is in so that it can be parsed consistently.

Steps to reproduce

Attempt to convert the date 30/06/2025 23:59:00 into time, it should ideally be parsed as mm/dd/yyyy format, but there's no easy way to parse that currently, except doing a somewhat hacky search and replace of the "/" with "-".

Proposed resolution

It'd be better if the plugin builder can specify the expected format of the date so that certain date formats can be easily converted to a date.

Remaining tasks

Provide issue fork/patch.

User interface changes

There should be a new "Custom date format" option on the plugin page. If it's not set, the previous strtotime() behaviour will be kept.

API changes

N/A

CommentFileSizeAuthor
#5 3530530.png227.04 KBmegachriz

Issue fork tamper-3530530

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

codebymikey created an issue. See original summary.

codebymikey’s picture

Component: Code » Plugins
Assigned: codebymikey » Unassigned
Issue summary: View changes
Status: Active » Needs review
codebymikey’s picture

Title: StrtoTime: Allow using a custom date format » StrToTime: Allow using a custom date format
megachriz’s picture

StatusFileSize
new227.04 KB

This is a great addition! I do wonder if we should fallback to strtotime() when parsing the date fails. Maybe provide that as an option? "Fallback to strtotime() if the date could not be parsed.". What do you think?

If you have applied the Tamper "String to Unix Timestamp" to the source "content" and set the date format to "d/m/Y" and then import the following CSV:

title,content
Foo,2020-09-01
Bar,01/11/2011
Qux,01-10-2011

This is the result after import (using the latest dev releases of Feeds and Feeds Tamper):

megachriz’s picture

I've added the fallback option. 🙂

codebymikey’s picture

Status: Needs review » Reviewed & tested by the community

The fallback option seems useful, the more flexibility the better.

I'd still personally recommend that users avoid using strtotime() blindly since as per the original issue, it can accidentally convert it into the wrong date, so your feed should ideally have a standardized format to convert from (however having the fallback option still wouldn't hurt for cases where the dates are entered manually).

All that being said, the code and tests looks good! RTBC.

megachriz’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for the review! I merged the code.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.