How to retrieve a date attribute in an email

Thanks guys, this helped me out alot. However I have one attribute left which contains a date. All except this value gets printed. Any ideas how a value containing a date is supposed to be printed in this matter?

Allright guys, I have figured out that I can get the product date by using getData instead of getAttributeText.

<?= $productdate = $_item->getProduct()->getData('productdate') ?>

This leads me to the next problem the date gets printed like: 2019-10-10 00:00:00. But the Magento system datapicker provides no option to set the time (eventhough the Magento manual says otherwise). This seems to be a known bug.

So does anybody know either how to fix the date/time picker or otherwise how to print the date so it doesn’t print time?

Hi @Dyo and welcome.

Thanks for updating us with your findings to your question. I’m pretty sure that Magento saves all dates to the MySQL DB in the format of 2019-10-10 00:00:00. It’s all about the retrieval method.

When you echo out the value, you’ll want to specify the format at this point otherwise you’ll just get the raw data.

So, in the below example $date represents the entry that saved in the DB. But in the echo line for format of that date is specified as Y/m/d, which results in 2019/10/10.

<?php
$date=date_create("2019-10-10 00:00:00");
echo date_format($date,"Y/m/d");
?>

This isn’t the solution to your issue, but hopefully this should point you in the right direction. You can find out more on PHP date_format() Funtion here. If you’re still struggling with this over the weekend, I’m sure @thomas will be able to give you a better answer than I.

1 Like

Hi,

<script type="text/javascript">
    require(['jquery', 'mage/calendar'], function($){
	    $("#input_date").calendar({
	        buttonText: "<?php echo __('Date') ?>",
  	        dateFormat: "d/m/yy",
	        minDate : 0
	    });
    });
</script>

Or you can change date format from PHP using the following code. Reference

$created_at = \DateTime::createFromFormat('j/n/Y', $created_at)->format("l");

Kind regards,

Thomas

2 Likes

Thank you for the the reply and your envolvement. Unfortunately I was unable to make it work with the provided sollutions.

When I try Craigs option: echo date_format($productdate,“Y/m/d”); It returns: “date_format() expects parameter 1 to be DateTimeInterface”. Somehow it seems that the returned getData is converted into a string value (var_dump confirmes this).

Then I tried Thomas’s alternatives, putting the javascript at the bottom of my script at: Magento_Sales\templates\email\items\order\default.phtml, but it didn’t change the output.

When I try the second option I get an error that the variable is undefined, I suspect it also has something to do with the data returned as string instead of datetime.

I guess persistence pays off, after some searching on Google I fixed it with something totally different. The getResource method is depricated, but this does what I was looking for.

$productdate = $_item->getProduct()->getResource()->getAttribute('productdate')->getFrontend()->getValue($_item->getProduct());
1 Like

As this is a deprecated solution, just be aware that one day a single update could break this function.