Jump to content

date strtotime question


sylunt1

Recommended Posts

Hello:

 

I am having issues with the below script.  It is not computing the date correctly and none of the other variables are getting passed to the db.  The object is to take tse_start (entered as mm/dd/yyyy) and add 28 days to it - this new date is to be entered as tse_estlive.  Any help is appreciated.

 

 

<?PHP

$tse_start   = $_POST['tse_start'];
$tse_id   = $_POST['tse_id'];
$tse_name = $_POST['tse_name'];
$tse_mentorname = $_POST['tse_mentorname'];
$tse_mgr  = $_POST['tse_mgr'];
$tse_grpid   = $_POST['tse_grpid'];
$tse_start   = $_POST['tse_start'];
$tse_level = $_POST['tse_level'];
$tse_weekorder  = $_POST['tse_weekorder'];
$tse_bootdone   = $_POST['tse_bootdone'];


$tse_start_db = date('Y-m-d', strtotime($tse_start));
$tse_est_live_db = date('Y-m-d', strtotime('$tse_start_db +28 days'));

      $db = mysql_connect("$server", "$user", "$password");
      mysql_select_db('mentor', $db) or die ('Cannot connect to $db_Database : ' . mysql_error());

echo "$tse_start_db";
echo "$tse_name $tse_mentorname $tse_mgr $tse_grpid $tse_start_db $tse_estlive_db $tse_level $tse_weekorder $tse_bootdone";

$sql =  "INSERT INTO tse (tse_name, tse_mentorname, tse_mgr, tse_grpid, tse_start, tse_estlive, tse_level, tse_weekorder, tse_bootdone)
         VALUES ('$tse_name', '$tse_mentorname', '$tse_mgr', '$tse_grpid', '$tse_start', '$tse_est_live_db', '$tse_level', '$tse_weekorder', '$tse_bootdone')";

$sql_result = mysql_query($sql,$db) or die ('Could not insert data');


echo('Data inserted successfully.');




mysql_close($db);

Link to comment
Share on other sites

Here's an alternative way of doing it.

 

$tse_start   = "01/12/2008";
$tse_start_db = explode("/",$tse_start);
$tse_start_db = $tse_start_db[2]."-".$tse_start_db[0]."-".$tse_start_db[1];
$tse_start_db_live = $tse_start_db[2]."-".$tse_start_db[0]."-".($tse_start_db[1] + 28);

Link to comment
Share on other sites

Did you replace these two lines of yours:

 

$tse_start_db = date('Y-m-d', strtotime($tse_start));
$tse_est_live_db = date('Y-m-d', strtotime('$tse_start_db +28 days'));

 

with these lines of mine?

 

//$tse_start   = "01/12/2008";
$tse_start_db = explode("/",$tse_start);
$tse_start_db = $tse_start_db[2]."-".$tse_start_db[0]."-".$tse_start_db[1];
$tse_start_db_live = $tse_start_db[2]."-".$tse_start_db[0]."-".($tse_start_db[1] + 28);

 

What I gave you was just an alternative to using strtotime

Link to comment
Share on other sites

The issue is that you are using single quotes around your string, so your variable is never being parsed before calculation.

 

Change this line:

<?php
$tse_est_live_db = date('Y-m-d', strtotime('$tse_start_db +28 days'));
?>

 

To this:

<?php
$tse_est_live_db = date('Y-m-d', strtotime("$tse_start_db +28 days"));

// or this...
$tse_est_live_db = date('Y-m-d', strtotime($tse_start_db . ' +28 days'));
?>

Link to comment
Share on other sites

p2grace - yes i replaced my lines with yours... no go...

 

obsidian - originally mine was using " and not ' - still same issue.  I tried the 2nd example and still nothin...  It is really perplexing...

 

Yes, what exactly is it doing? I see you are building your entire script off the assumption that $_POST['tse_start']... have you tried echoing that variable out to make sure that it carries what you think it does?

Link to comment
Share on other sites

it isnt echoing anything out - once I put the date modification stuff in there it wont echo anything else except an incorrect modified date.

 

If the start is 01/01/2008 it echos 01/21/2008 - thats it.  I have put in markers before and after the code and those get echoed, but all the other variables go away...

Link to comment
Share on other sites

You aren't using strtotime correctly. If you want to add 28 days to a TIMESTAMP its

 

$now = time();
$tsTwentyEightDaysLater = strtotime("+28 day",$now); /*note that by default strtotime uses the current time() as the second argument. I'm just putting that in for clarity. */

//In your case, you want to first convert your mm/dd/yyyy date to a TIMESTAMP

$tsUserDate                      = strtotime($tse_start); //gives us a timestamp
$tsTwentyEightDaysLater    = strtotime("+28 day",$tsUserDate); //gives us a timestamp
$mdyTwentyEightDaysLater = date("m/d/Y",$tsTwentyEightDaysLater); //gives us an m/d/Y formatted date

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.