Jump to content

Another date question...


php_novice2007

Recommended Posts

Hi,

 

So now I've got a string in the form $s = "yyyy-mm-dd hh:mm:ss" and a positive integer (might be 0) representing a number of days $d = 5.

 

I want to create a new string $s1 that is $s- $d

 

eg $s = "2007-09-01 12:24:22", $d = 5 ==> $s1 = "2007-08-27 12:24:22"

 

I'm thinking of something like

 

$s1 = date(strtotime($s)) - date(strtotime($dd));

 

but I have no idea how to represent 5 days into yyyy-mm-dd hh:mm:ss format, it doesnt actually make sense to convert a day to that form...

 

How should I be doing this?

 

 

Link to comment
Share on other sites

Me personally I find it easier to just keep all dates in unix timestamp. You don't have to worry about converting them, you can perform mathematical equations on them and one string gives you seconds, minutes, hours, day, month & year.

 

Do yourself a favor, just keep them all in unix timestamp format in the db and in the script. Then when you need to display in a certain format, just use date().

 

When I first started playing with MySql, I thought "hey let's keep dates in the *preferred* mysql format", I quickly realized that its a pain in the ass to try and manipulate that date string. If your just storing and displaying, then ok no prob... but as you can see the moment you need to do math with time you need a unix timestamp. So cut the middle man and just store them in that format to begin with and you'll save some headache.

 

Nate

Link to comment
Share on other sites

Are ya looking for something like this?  outputs 2007-08-27 12:24:22.

 

mktime() is mainly used for making a unix timestamp that is in the future or past. If you wanted to get the day of the week that May 4th fell on in 1985, mktime() could calculate that for ya.

 


<?php

$seconds_in_a_day=86400; // the num of seconds in a day

$s = "2007-09-01 12:24:22"; //time string
$d = 5; // days to factor in
$dd=$d*$seconds_in_a_day; get the number of seconds by multiplying days, by seconds in a day

$s1 = strtotime($s)-$dd; //perform our subtraction

echo date("Y-m-d h:i:s",$s1); // echo the date in the format we want.
?>

 

The key here is strtotime(). That will take your date string and turn it into a unix timestamp and then you can do your math with it. Then you just have to convert it back to the format you need.

 

Nate

Link to comment
Share on other sites

Or:

 

<?php
$date1 = "2007-09-01 12:24:22"; // example
$num_days = 5; // example

$timestamp = strtotime($date1);
$newtime = $timestamp - 86400 * $num_days;
$newdate = date("Y-m-d h:i:s", $newtime);

echo $date1. " minus ". $num_days. " days is ". $newdate;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.