0

I get a string date variable out of an extension in WordPress and need to change it into a date variable with the format (Ymd) to enter it into another db.

Actually i get the value out of the WordPress ACF-Extension. This works, the format inside this field is d.m.Y (01.12.2019):

$task_deadline = get_field( 'task_deadline', $task_id );

Then i try to change the variable into a date variable. This is not working i always get the output 01.01.1970.

$task_deadline_date = date('d.m.Y',$task_deadline);

After that i would like to change the variable format to Ymd. This is not working:

$task_deadline_date_format = date('d.m.Y',$task_deadline);

5 Answers 5

1

The second parameter to date() is a timestamp, not a time-string. So you will need to use strtotime() to get the timestamp from your date first.

$task_deadline_date_format = date('dmY', strtotime($task_deadline));
Sign up to request clarification or add additional context in comments.

Comments

1
$date = DateTime::createFromFormat('d.m.Y', '01.12.2019');
echo $date->format('Ymd');

Comments

0

Change the date format to

 $task_deadline_date_format = date('Ymd',strptime($task_deadline, '%d.%m.%Y'));

Comments

0

try:

$date=date_create("01.12.2019");
echo date_format($date,"Ymd");

refer link : https://www.w3schools.com/php/showphp.asp?filename=demo_func_date_create

Comments

0

We can change PHP string variable to date format by using php date() and strtotime() functions

Syntax to use these functions are as below:

date('Ymd',strtotime($task_deadline));

you can use other formats(instead of Ymd if required)such as: 1) Y-m-d 2) Y/m/d

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.