I'm having a little bit of trouble with getting an incremented date into an MySQL database. The field is of type DATE.
First of all I increment the date with the following code:
if($_POST['membershipLength'] == "6 Months") {
$renew = mktime(0, 0, 0, date("m")+9, date("d"), date("y"));
}
Then I wish to enter: date("m/d/y", $renew) into the column in the database.
Can anyone see where I'm going wrong? The value it inserts consists of 0s which I'm assuming is the default.
The MYSQL date field has the following format "Y-m-d" so something like "2010-11-04". You're trying to insert a value as "2010/04/11". Change your first parameter to "Y-m-d" and it should work :)
Try this:
date ("Y-m-d H:i:s", $renew);
MySQL likes dates in a specific format.
the date format in MySQL is YYYY-MM-DD and you are inserting M-D-Y try with Y-M-D it will resolve your problem
I tried your code and working fine, other than one thing. When you want to add 6 months, why are you adding 9 in months?
if($_POST['membershipLength'] == "6 Months") {
$date = mktime(0, 0, 0, date("m")+6, date("d"), date("Y"));
$renew = date("Y-m-d", $date);
} elseif($_POST['membershipLength'] == "9 Months") {
$date = mktime(0, 0, 0, date("m")+9, date("d"), date("Y"));
$renew = date("Y-m-d", $date);
} elseif($_POST['membershipLength'] == "12 Months") {
$date = mktime(0, 0, 0, date("m")+12, date("d"), date("Y"));
$renew = date("Y-m-d", $date);
}
Thanks for your help everyone. The mistake I was making in addition the corrections you all suggested was that I was checking for a value that didn't match. Silly me!
Related
I have some data that makes use of date("j/n/y") format i.e date for today is 23/1/15
I have tried
echo strtotime($today);
but this does not give me the timestamp i want.How would i convert a date in date("j/n/y") format to epoch?.
Use DateTime::createFromFormat() to read the date format and then use DateTime::getTimestamp() to format it as a timestamp.
$date = DateTime::createFromFormat('j/n/y', '23/1/15');
$epoch = $date->getTimestamp();
I think you're looking for the mktime function in PHP. It goes a little like this:
$timestamp = mktime(0,0,0,0,0,0);
Where, in order, the arguments are: hour, minute, second, month, day, year. So, in your case:
$today = mktime(0, 0, 0, 1, 23, 2015);
// Would return the timestamp for Jan. 23rd, 2015 at 12:00:00 am (I think)
If you're looking for a dynamic right now timestamp, you may use date() in each of the arguments of mktime. For example:
$rightnow = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
// Would return the timestamp for Jan. 23rd, 2015 at 10:57:25 am.
But, as John Conde says, it requires you break apart the date before you can use it, so it may not be as efficient.
Hope that helps!
Just to have another approach this one would be good for 85 more years.
$date = date('j/n/y', time());
list($day, $month, $year) = explode("/", $date);
$date = "20" . $year . "-" . $month . "-" . $day;
echo date('m/d/Y', strtotime($date));
I have a problem with find the current date from past mktime. In PHP I find the current date using date("j");. Here I need, suppose my date was in the past year like mktime(0, 0, 0, 2, 1, 2008), then here how can I find the current date of this particular past month.
Either as #octern's solution, or you can do
$day = date('j', strtotime("-2 months"));
or
$day = date('j', strtotime('-30 days'));
depending on your need.
You may also want to refer to strtotime() manual.
Try this:
$date = getdate(mktime(0, 0, 0, 2, 1, 2008));
$day = $date['mday'];
Or just:
$day = date('j', mktime(0, 0, 0, 2, 1, 2008))
I'm trying to create an date with
$date_end = mktime(0, 0, 0, date('m'), date('d')+7, date('Y'), $date_set);
The output is today + 7 days instead of the date given + 7.
The manual says nothing about mktime() taking a date as argument.
Use strtotime("+7 days", $date_set).
$date_end = mktime(0, 0, 0, date('m', $date_set), date('d', $date_set)+7, date('Y', $date_set));
is, I believe, what you were trying to accomplish (assuming $date_set is a timestamp). Else, #Kristian's suggestion I believe is a good one.
Why are you passing a $date_set variable, and why are you using mktime if you already have the time?
Simply add 7 days: $date_end = $date_set + (7 * 86400);
I want a First Day Of a week say I have 45th week. Now I want to have date of the first sunday of this week.
Can any one suggest me how I can go about this ?
Thank you
Searching around a bit, the most suggested is:
$year = "2009"; // date("Y");
$week = "45"; // date("W");
$firstDayOfWeek = strtotime($year."W".str_pad($week,2,"0",STR_PAD_LEFT));
print("The first day of week ".$week." of ".$year." is ".date("D, d-m-Y",$firstDayOfWeek));
Basically this comes down to letting strtotime do the work for you. You fill in "2009W45" and it'll retrieve the date for you.
Pitfall here is that the week needs to be in a 2 digit format. So week 1 needs to be 01, hence the str_pad to zero-fill it.
you should strtotime and date function
some code like that
$next_sunday = strtotime('next sunday');
$next_week = strtotime('+1 week');
$next_sunday_of_next_week = strtotime('next sunday', $next_week);
hope this helps
If you have the month, day and year:
$timestamp = mktime(0, 0, 0, $month, $day, $year);
echo date('c', $timestamp) = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year);
you could also do it this way if you have the timestamp:
echo $date('c', mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
From: http://pinoytech.org/blog/post/get-the-first-day-of-the-week-with-an-exact-date
date($str_format, strtotime($year."W".$week."1"))
Where $str_format is the output formate according to the PHP date() function.. I use 'M d Y'
I've rarely touched PHP date functions,
and now I need to do the follows:
get current date,
get date of three days later
get date of three weeks later
get date of three month later
get date of three years later
and finally to implement such a function:
function dage_generate($number,$unit)
{
}
$unit can be day/week/month/years
http://uk.php.net/strtotime can do most of that:
strtotime("today")
strtotime("+ 3 days")
strtotime("+ 3 weeks")
strtotime("+ 3 months")
strtotime("+ 3 years")
The function would be something like:
function dage_generate($number,$unit)
{
return strtotime("+ ".$number." ".$unit);
}
http://us.php.net/manual/en/function.date.php
Note towards the bottom of the page:
Example #3 date() and mktime() example
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
Using strtotime() you can do this easily.
$now = time();
$threeDays = strtotime("+3 days");
$threeWeeks = strtotime("+3 weeks");
$threeMonths = strtotime("+3 months");
$threeYears = strtotime("+3 years");
Each of those variables will be an integer which represents the unix timestamp at that point in time. You can then format it into a human readable string using date().
echo date('r', $threeWeeks);
// etc...
Use date_create() to create a DateTime object, then use the add method.
See the examples on the page for the add() method, they contain all you need.
PHP Date/Time Functions with examples