dqt83336 2014-10-27 11:56
浏览 37

重新计算存储为varchar类型的日期

I have a table that contains expiry dates for software modules. They are stored as type varchar(32). The database also stores dates one month prior to expiry and one day prior to expiry. When a license is approaching expiry the client is notified by message stack when they go into site admin.

What i want is to be able to edit the expiry date so that additional months or even years can be added to a clients license. The issue is that it also needs to update the other dates relative to the new expiry or they will get warnings when they shouldn't.

So field 'edate' contains a date stored as 25-10-2015, 'wdate' contains 25-09-2014 and 'fwdate' contains 24-10-2015

How do you take the date stored in the db and make it usable with code as seen below which originally calculated the dates.

$todays_date = strtotime("now");
$delta_eleven = strtotime("+11 months");
$delta_364 = strtotime("+1 year -1 day");
$delta_year = strtotime("+1 year");

$idate = date("d-m-Y", $todays_date);
$wdate= date("d-m-Y", $delta_eleven);
$fwdate = date("d-m-Y", $delta_364);
$edate = date("d-m-Y", $delta_year);
  • 写回答

1条回答 默认 最新

  • duanming7833 2014-10-27 12:00
    关注

    On database side you can use STR_TO_DATE(). Explanation of the second parameter can be found here. In your case

    SELECT STR_TO_DATE(your_date_column, '%d-%m-%Y') FROM your_table;
    

    To add or subtract something you can use

    SELECT STR_TO_DATE(your_date_column, '%d-%m-%Y') + INTERVAL 11 MONTH
    FROM your_table;
    

    or

    SELECT STR_TO_DATE(your_date_column, '%d-%m-%Y') + INTERVAL 1 YEAR - INTERVAL 1 DAY
    FROM your_table;
    

    It would be best if you added a column with the appropriate datatype (date, datetime or timestamp), update the new column with the str_to_date() function and then drop the old varchar column containing your dates. This makes work much easier and you can use indexes on the column if necessary.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c