Get only date not time from date_entered field

$SugarQuery = new SugarQuery();
$SugarQuery->from(BeanFactory::getBean('Contacts'));
$documents = $SugarQuery->join('documents')->joinName();
$SugarQuery->select(array("$documents.dms_document_id_c","$documents.document_name","$documents.date_entered"));
$SugarQuery->where()->equals('id', $args['km']);
$SugarQuery->where()->equals("$documents.document_name", $args['document_type']);
$SugarQuery->orderByRaw("$documents.date_modified");
$results = $SugarQuery->execute();

output:-

 {
            "dms_document_id_c""9765444",
            "document_name""Crop",
            "date_entered""2020-12-23 10:20:03"
        }

How to get only date not time from date_entered in $results?

Like,

 "date_entered""2020-12-23 "

  • Hi ,

    There are various way to do this. 

    1. Simple explode can give you the result

    Fullscreen
    1
    explode(" ", $bean->date_entered)[0];
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    2. A little safer approach using global $timedate

    Fullscreen
    1
    (new \TimeDate($bean->date_entered))->format(\TimeDate::DB_DATE_FORMAT);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    3. A little more advanced it do it in select of the query via substring_index procedure of MYSQL

    More info can be found here:
    dev.mysql.com/.../string-functions.html

    Hope it helps.

    Tevfik Tümer
    Sr. Developer Support Engineer

  • You may have to consider whether the date should be in the local timezone because $bean->date_entered will be UTC

  • $SugarQuery->select->fieldRaw("date($documents.date_entered)", 'date_entered');