Need Help Creating a Date in EspoCRM Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huseyinerisen
    Junior Member
    • Feb 2025
    • 2

    Need Help Creating a Date in EspoCRM Script

    There is an entity named "prim" in EspoCRM. This entity has two fields as cOrderingDate (date), cPaymentDate (date).

    1 month should be added to the month value found in the date value entered in the cOrderingDate field.

    Then, the 15th of the month in the calculated new date should be written in the premium payment date field.

    Examples;
    if the order date is March 1, the premium payment date should be April 15,
    if the order date is March 31, the premium payment date should be April 15.

    In short, premiums are paid on the 15th of the following month.
    Additional information: EspoCRM version 8.4.2.

    I want to realize this request by writing to the "before save custom script" field with the formula feature in the "prim" entity.

    Thanks in advance
  • item
    Active Community Member
    • Mar 2017
    • 1518

    #2
    Hi,
    if i see, not possible out-of-box formula function, but you can do :

    first, you must find "first day of next month'", so you won't have bug with february, and 30 or 31 day

    after, you can add "15 day"

    It's easy to make a custom formula. : https://docs.espocrm.com/development...on-in-formula/

    PHP Code:
    $date = new \DateTime('myOrderDate');
    $date->modify('first day of next month');
    $date->modify('+15 days');
    return 
    $date->format('Y-m-d'); 
    Last edited by item; 04-10-2025, 02:21 PM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment


    • huseyinerisen
      huseyinerisen commented
      Editing a comment
      Thanks for your time. Solved it.
  • yuri
    Member
    • Mar 2014
    • 8845

    #3
    It can be done with formula.

    Code to try in the Formula Sandbox. You can change the $orderDate to test how it works for different dates.

    Code:
    $orderDate = '2025-03-31';
    
    $day = datetime\date($orderDate);
    $monthStart = datetime\addDays($orderDate, (- $day + 1));
    $nextMonthStart = datetime\addMonths($monthStart, 1);
    $paymentDate = datetime\addDays($nextMonthStart, 15 - 1);
    
    output\print($paymentDate);
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment


    • huseyinerisen
      huseyinerisen commented
      Editing a comment
      Thank you Yuri, it worked and I am impressed by your way of thinking.

      May I have your contact information for a possible professional collaboration?

    • item
      item commented
      Editing a comment
      Hi Huseyin
      i agree, Yuri is a many many hight level... on top
      i follow his work since long years.

      For info : EspoCrm have a portal.espocrm.com .. there i have see we can buy "hours (something so)" .. but don't know how buy.
      Last edited by item; 04-11-2025, 11:26 PM.
Working...