在Shopify中向日期添加天数(不包括周末) [英] Adding days to a date excluding weekends in Shopify

查看:58
本文介绍了在Shopify中向日期添加天数(不包括周末)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些代码来估计Shopify中的发货日期.我一直在学习入门,但是在用Ruby编写一些逻辑时遇到了麻烦.我不确定Shopify模板是否使用Ruby或Ruby on Rails.

I'm trying to write some code to estimate a shipping date in Shopify. I've been learning liquid but am having trouble writing some logic in Ruby. I'm not sure if Shopify templates use Ruby or Ruby on Rails.

我想获取今天的日期,并根据输入变量添加3或5天,请谅解周末.这是我在PHP中的操作方式:

I want to get todays date and add either 3 or 5 days based on a input variable, excusing weekends. Heres how I'd do it in PHP:

$orderDate = date('Y-m-d');
$personalized = true;

$orderDays = ($personalized ? 4 : 2);
(date('H') > 12 ? $orderDays++ : $false);

$d = new DateTime( $orderDate );
$t = $d->getTimestamp();

// loop for X days
for($i=0; $i<$orderDays; $i++){

    // add 1 day to timestamp
    $addDay = 86400;
    // get what day it is next day
    $nextDay = date('w', ($t+$addDay));
    // if it's Saturday or Sunday get $i-1
    if($nextDay == 0 || $nextDay == 6) {
        $i--;
    }
    // modify timestamp, add 1 day
    $t = $t+$addDay;

}

$d->setTimestamp($t);
echo $d->format( 'Y-m-d' ). "\n";

为Shopify液体编写此代码的最佳方法是什么?

Whats the best way to write this for Shopify liquid?

推荐答案

在Liquid中使用 date 过滤器.

Use the date filter in Liquid.

您在Ruby中使用的日期格式等效于:

The date format you are using in Ruby is equivalent to:

{{ "now" | date: "%Y, %m, %-d" }}

一天是86400秒.从现在开始添加3天的示例,其格式为:

A day is 86400 seconds. Example to add 3 days from now and format:

{% assign days = 3 | times: 86400 %}
{{ "now" | date: "%s" | plus: days | date: "%Y, %m, %-d" }}

注意:您需要先将 date:%s" 过滤器应用到"now",Liquid才能了解您使用的是当前时间戳,而不是字符串"now".

Note: You need the date: "%s" filter applied first to "now" for Liquid to understand that you are using the current timestamp and not the string "now".

一种检查周末是否下订单的方法是:

A way to check if an order is made on a weekend would be:

{% assign wday = order.created_at | date: "%a" %}
{% if wday == 'Sat' or wday == 'Sun' %}
    el Weekendo
{% endif %}

这篇关于在Shopify中向日期添加天数(不包括周末)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆