Changing Year Specifically in date() - php

I wish to do an operation on a given date string which needs to change the year of that date to, let's say, 2000. Is there a way to accomplish this feat using PHP's date()?
$old_date = '2017-05-05';
// Needs to change 2017 to 2000.
$new_date = ?
I know one method involves using strtotime($old_date. '-17 years') but its not applicable in every case, such as 2018 which will become 2001. Is there an alternate solution that avoids hardcoding the number of years to subtract?
NOTE: I'm assigning a date string to $old_date for convenience. The actual date is fetched from a table.

Subtract years after millennium
strtotime($old_date . ' -' . date('y', strtotime($old_date)) . 'years')

$time = strtotime($old_date);
$year = 2000;
$new_date = date("Y-m-d", mktime(0, 0, 0, date('n', $time), date('j', $time), $year));

Related

How can i convert date("j/n/y") to epoch

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));

PHP and or mysql: Make a date from some variables

Assume that i have two variables in php:
$year
$month
Then I want to make another variable:
$date
which:
$date=$year-$month-25
So, if I have 2012 in $year and 7 for $month, $date will be 2012-07-25.
Actually, I will compare it with some date in MySQL.
$year and $month are inputted by user.
anybody have a solution?
The solution either how to make $date or anything as long it can be comparred with a date in mysql.
Thanks before. ^^
You can make a unix timestamp through this:
$myDate = mktime(0, 0, 0, $month, 25, $year);
This is a pretty useful thing to have, as you can format it into all sorts of nice via:
echo date("Y-m-d", $myDate);
// Prints something like: 2012-07-25
or
echo date("l", $myDate);
// Prints something like: Monday
or
date('l jS \of F Y h:i:s A', $myDate);
// Prints something like: Monday 8th of August 2005 03:12:46 PM
You can do as follows
$complete_date = $year."-".$month."-25";
which gives you 2012-7-25
Please, read the "php manual" for concat your PHP string.
it's not
$date = $year-$month-25;
it is
$date = $year . '-' . $month . '- 25';
or
$date = $year . "-" . $month . "- 25";
but simple quote is more optimize for php string.
The solution either how to make $date or anything as long it can be
comparred with a date in mysql
The key here is use of strtotime to create and compare.
MySQL dates can be converted to integer through the use of strototime:
strtotime($mysql_date);
Then you can get time() and compare to two:
time()<>strtotime($mysql_date) // then the two dates are not equal.
You can use mktime function
$date = date('Y-m-d',mktime(0,0,0,$month,25,$year));
Well, I would use mktime to get the timestamp of the date ( http://php.net/manual/de/function.mktime.php ) and use the command unix_timestamp(date(yourfield)) in mysql to compare them.
(the date() withing unix_timestamp is only required when you save datetime values and not pure date values)
Since mysql dates are usually in this format Y-m-d by default, you can use $thedate = date('Y-m-d',mktime(0,0,0,$month,25,$year)); where $month and $year are based on the user input. Of course you have to make the user input it in the format you want by using select/lists.

convert a date in this format '031012' into a date that I can add days to

I have a date in this format 030512 (ddmmyy).
But I'm having trouble with converting this to a date usable format I can add days to.
Basically.. I extracted the date above from a text file, Now I need to be able to add a number of days to it. But I am having trouble parsing the date in this format.
Is there another way of doing this rather then something like this:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
$date_after = $day . "-" . $month . "-".$year;
// Now i need to add x days to this
$total_no_nights = 010; // must be this format
$days_to_add = ltrim($total_no_nights,"0"); // 10, remove leading zero
// how do i add 10 days to this date.
You can do this (php >= 5.3):
$date = DateTime::createFromFormat('dmy', '030512');
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://www.php.net/manual/en/datetime.createfromformat.php
For php < 5.3 :
$dateArray = str_split('030512', 2);
$dateArray[2] += 2000;
echo date("d/m/Y", strtotime('+1 day', strtotime(implode('-', array_reverse($dateArray)))));
try this using the month/day/year you already have:
$date = "$month/$day/$year";
$change = '+10 day';
echo date("d/m/Y", strtotime($change, strtotime($date)));
Assuming the date will always be in the future (or at least after 1st Jan 2000), you're not far wrong:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
// dd-mm-yy is not a universal format but we can use mktime which also gives us a timestamp to use for manipulation
$date_after = mktime( 0, 0, 0, $month, $day, $year );
// Now i need to add x days to this
$total_no_nights = "010"; // must be this format
$days_to_add = intval( $total_no_nights ); // No need to use ltrim
// Here's the "magic". Again it returns a timestamp
$new_date = strtotime( "+$days_to_add days", $date_after );
Using the DateTime object would be easier but you say you're not on PHP5.3.
You can't do date manipulation with strings becase, well, they are not dates. In PHP, you can use Unix timestamps (which are actually integers...) or DateTime objects. In you case:
$timestamp = strtotime('-10 days', mktime(0, 0, 0, $month, $day, $year));
echo date('r', $timestamp);
... or:
$object = new DateTime("$year-$month-$day");
$object->modify('-10 days');
echo $object->format('r');
Also, please note that 010 is an octal number that corresponds to 8 in decimal.
using the convert function in sql the date can be obtained in appropriate format.
anter that operations can be performed in php to manipulate the date. i hope that answers your query.

parse UTC timestamp in PHP for only year, month, and date?

what is the best way to take a UTC timestamp (integer) and parse it for only the year, month, and day in PHP? Thanks
Use the PHP date() function.
<?php
$timestamp = strtotime("2011-9-12 05:48:00");
$year = date('Y', $timestamp);
$month = date('m', $timestamp);
$day = date('d', $timestamp);
$newTimestamp = mktime(0, 0, 0, $month, $day, $year);
DateTime class is pretty good for this kind of task
<?php
// for PHP5.2+ though
var_dump(date_create('2011-9-12 05:48:00')->setTime(0, 0, 0)->format('U'));
// if input is in integer already
$input = 1315806480;
var_dump(date_create("#{$input}")->setTime(0, 0, 0)->format('U'));
<?php
$time = strtotime(date('Y-m-d', *timestamp*));
?>
EDITED:
The date() function takes your timestamp and represents it in a human readable string with only the year, month, and date (i.e. "2014-07-25") and omitting the hour, minutes, and seconds.
Then the strtotime() function takes that human readable string and turns it back into your timestamp, effectively making the time at 00:00 on that day.

php - mktime or strtotime?

I'm trying to convert 2010-02 to February, 2010. But, I keep getting December, 1969
I've tried using mktime, strtotime, and some combination of the two, but still haven't been able to do it...
This is what I tried most recently...
$path_title = date('F, Y', mktime(0,0,0,2,0,2010));
This would be a way to do it:
$dateString = '2010-02';
list($year, $month) = explode('-', $dateString);
$timeStamp = mktime(0, 0, 0, $month, 1, $year);
echo date('F, Y', $timestamp);
Another way would be:
$dateString = '2010-02';
$timestamp = strtotime($dateString . '-01');
echo date('F, Y', $timestamp);
strtotime can't handle ambiguous dates like "2010-02", but if you make it a full date it should work.
Otherwise, you may want to look into something like DateTime::createFromFormat.
Try this:
$str = '2010-02';
echo date('F, Y',mktime(0,0,0,substr($str,-2),1,substr($str,0,4)));
You have to make sure you use valid values to mktime(). In your example that you edited into the question, you have 0 as the day, which is effectively the first day minus one, which puts you into the previous month.

Categories