Reputation: 1397
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);
Upvotes: 0
Views: 763
Reputation: 162
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
Upvotes: 0
Reputation: 189
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
Upvotes: 0
Reputation: 179
$date = DateTime::createFromFormat('d.m.Y', '01.12.2019');
echo $date->format('Ymd');
Upvotes: 1
Reputation: 26490
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));
Upvotes: 1
Reputation: 5713
Change the date format to
$task_deadline_date_format = date('Ymd',strptime($task_deadline, '%d.%m.%Y'));
Upvotes: 0