How to remove (YYYY) from date-string in PHP? - php

I am making a function that remembers a users birthday, and im storing the birthdate in my database in this format "08/12/1988".
Now how do I remove the "YYYY", so that I end up with only day and month? 08/12
Because that would allow me to make a if statement like this:
THIS DIDN'T WORK:
$date_today = date("d-m");
if($string_from_db_without_year == $date_today){
echo"Congrats, it's your birthday!";
}
THIS WORKED:
$string_from_db = $bruger_info_profil['birthday'];
$date = strtotime($string_from_db);
$converted_date_from_db = date('m/d',$date);
echo $converted_date_from_db;

<?php
$date = strtotime('08/12/1988');
echo date('d/m',$date);
?>

Try with:
$input = '08/12/1988';
$output = date('d-m', strtotime($input));

$time = strtotime( "08/12/1988");
$birthday = date("d-m", $time);
Or
list($month, $day, $year) = explode('/', "08/12/1988");
$birthday = $day.'-'.$month;

Related

Avoid loop for Date add by day?

I am using Carbon to add number of days, it there a way to avoid using for and/or while loop?
Add the numbers of days ($skipDayBy) and add the number of days if found in $excludeDatesPublic or $excludeDatesManual?
For example working demo:
function calculateDate($skipDayBy = 0) {
$excludeDatesPublic = ['2019-08-28'];
$excludeDatesManual = ['2019-09-01'];
$date = Carbon::now();
for($i = 0; $i < $skipDayBy; $i++) {
$date = $date->addDays(1);
while(in_array($date->toDateString(), $excludeDatesPublic) || in_array($date->toDateString(), $excludeDatesManual))
{
$date = $date->addDays(1);
}
}
return $date->toDateString();
}
echo calculateDate(4);
Returned 2019-09-02 as expected if today date is 2019-08-27.
Maybe you're looking for https://github.com/kylekatarnls/business-day that allows you to add days skipping holidays.
Alternatively, you can use the periods:
$skipDayBy = 5;
$excludeDatesPublic = ['2019-09-01'];
$excludeDatesManual = ['2019-09-04'];
$exclude = array_merge($excludeDatesPublic, $excludeDatesManual);
$date = CarbonPeriod::create(Carbon::now(), $skipDayBy)
->addFilter(function (Carbon $date) use ($exclude) {
return !in_array($date->format('Y-m-d'), $exclude);
})
->calculateEnd();
var_dump($date); // 2019-09-06 18:50:17 if run at 2019-08-31 18:50:17
With PHP shortly
$day='2019-12-12';
$date = date('Y-m-d', strtotime($day . " +4 days"));
echo $date;
Output
2019-12-16
Or u can use
$date= date('Y-m-d', strtotime('+4 days', strtotime($day)));

PHP 01/01/1970 Issues with Date Fields

I am extracting date from a string. Below is my code. I am getting only January 1, 1970 at the end. Please help me to fix this. your help would be appreciated.
$current = 'DATE OF : 23/03/1951 BENCH:';
$DATEOF = preg_replace('/(.*)DATE OF (.*?)BENCH:(.*)/s', '$2', $current);
if (!is_null($DATEOF)) {
$oldDate = $DATEOF;
$oldDateReplace = str_replace(array('!\s+!', '/^\s+/', '/\s+$/',':'), array('','','',''), trim($oldDate));
$date = ''.$oldDateReplace.'';
$timestamp = strtotime($date);
if ($timestamp === FALSE) {
$timestamp = strtotime(str_replace('/', '-', $date));
}
echo $newDate = date("F j, Y",$newDateM);
}else{$newDate = '';}
// print this date only January 1, 1970 i want this date 23/03/1951
Use below code
$current = 'DATE OF : 23/03/1951 BENCH:';
$DATEOF = preg_replace('/(.*)DATE OF (.*?)BENCH:(.*)/s', '$2', $current);
if (!is_null($DATEOF)) {
$oldDate = $DATEOF;
$oldDateReplace = str_replace(array('!\s+!', '/^\s+/', '/\s+$/',':'), array('','','',''), trim($oldDate));
$date = ''.$oldDateReplace.'';
$timestamp = strtotime($date);
if ($timestamp === FALSE) {
$timestamp = strtotime(str_replace('/', '-', $date));
}
echo $newDateM = date("m/d/Y",$timestamp);
echo $newDate = date("F j, Y",$timestamp);
}else{$newDate = '';}

Apply all year's dates as keys into an array

I have an array with 364 values. Each value of the array represents a status of each day of the year.
The array looks like this:
array([0]=>'something',
[1]=>'something_else'
....
[364]=> 'the_end_of the year')
What i want to do is to replace all the array keys with the dates of the year. Something that looks like this:
array([2015-01-01]=>'something',
[2015-01-02]=>'something_else'
....
[2015-12-31]=> 'the_end_of the year')
You can try with -
$begin = new DateTime('2015-01-01');
$end = new DateTime('2015-12-31');
$end = $end->modify('+1 day');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$new = array();
foreach($daterange as $date){
$new[$date->format("Ymd")] = "Your values";
}
Probably you can use something like this.
<?php
date_default_timezone_set('UTC');
// Start date
$date = '2015-1-1';
// End date
$end_date = '2015-12-31';
$result = array();
while (strtotime($date) <= strtotime($end_date)) {
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
$result[$date]="your info";
}
echo "<pre>";print_r($result);
?>
ok here is an example
<?php
$new = array();
for($i=0; $i<365; $i++) {
$key = date('Y-m-d',strtotime('01.01.2015 +'.$i.' days'));
$new[$key] = $i;
}
var_dump($new);
die;
and in your case
$new = array();
foreach($array as $i=>$value) {
$key = date('Y-m-d',strtotime('01.01.2015 +'.$i.' days'));
$new[$key] = $value;
}
var_dump($new);
die;
You can iterate thru array and convert current day to a DateTime object using createFromFormat method like this
$d = DateTime::createFromFormat('z Y', '110 2015');
where 110 is the day of the year and 2015 is year against which to create the date to. From there you should be able to set the keys to whatever format you please.

Issue in get the start and end dates of all weeks between two dates in Mysql

i want to display all week dates in between choosing start date to end date so i try following code
<?
$signupweek='2015-05-21';
/*start day*/
for($i = 0; $i <7 ; $i++)
{
$date = date('Y-m-d', strtotime("-".$i."days", strtotime($signupweek)));
$dayName = date('D', strtotime($date));
if($dayName == "Sun")
{
echo "start day is ". $date."<br>";
}
}
?>
when i run above code it gives only date like before 7 days to start date but i want to result like below
sDate = '2013-02-25',
eDate = '2013-03-25';
Output
2013-02-25
2013-03-04
2013-03-11
any idea how can i make it possible ? your suggestions is appreciable.
EDIT
As per someOne answer i get result which i want but now i make some little bit change on that like , now if i entered start_date =2015-04-01 and end_date=2015-05-01 so i need output like below
2015-04-08
2015-04-15
2015-04-22
2015-04-29
i don't need start_date=2015-04-01 in my output list .is it possible in below function ?
As per the edit and the request of the OP, the following new function is provided, which enables the user to specify whether or not to include the starting or ending dates in the final result using the optional arguments of $includeFrom and $includeTo respectively:
function getWeeklyDates2($from, $to, $includeFrom = false, $includeTo = true) {
$from = strtotime($from);
$to = strtotime($to);
if($includeFrom===false)
$from = strtotime("+7 day", $from);
$dates = array();
$current = $from;
while($includeTo ? $current <= $to : $current < $to) {
$dates[] = $current;
$current = strtotime("+7 day", $current);
}
return $dates;
}
$dates2 = getWeeklyDates2('2015-04-01', '2015-04-29', false, true);
foreach($dates2 as $date)
echo date("Y-m-d", $date) ."<br />";
So you want to get the dates of every next week:
<?php
function getWeeklyDates($from, $to) {
$from = strtotime($from);
$to = strtotime($to);
$dates = array();
$current = $from;
while($current <= $to) {
$dates[] = $current;
$current = strtotime("+7 day", $current);
}
return $dates;
}
$dates = getWeeklyDates('2013-02-25', '2013-03-25');
foreach($dates as $date)
echo date("Y-m-d", $date) ."<br />";
?>

PHP Datetime Convert slash to minus

I want to convert my datetime 08/11/2013 to 2013-08-11.
I was trying this, but it did not work as it suppose to.
$date = $_POST['gt_date']; // Getting 08/11/2013
$date = explode("/", $date); // Exploding the / character into array
$datetime = $date[2] + '-' + $date[0] + '-' + $date[1];
Any Ideas?
You can pass it to DateTime to format it for you.
$date = new DateTime('08/11/2013');
echo $date->format('Y-m-d');
I don't know what the + does, but changing your code to the following will work:
$datetime = $date[2].'-'.$date[0].'-'.$date[1];
Edit: I guess the + was adding them together. Was the result you were getting 2032?
Here's a function that will return your format so that you don't have to repeat your code.
function makeDateFormat($date) {
$date = $_POST['gt_date'];
$date = explode("/",$date);
$new_date = array($date[2], $date[0], $date[1]);
$date = implode("-",$new_date);
return $date;
}
This issue is caused by the "+" operator, replace it with "." operator. You could use this function:
function formatDate($date) {
$arr = explode("-",$date);
$year = $arr[2];
$month = $arr[1];
$day = $arr[0];
return $year."-".$month."-".$day;
}
Nice function but correction is this below if you want to covert format as well:
echo makeDateFormat('13/02/23','Y-m-d');
function makeDateFormat($date,$date_format) {
$date = explode("/",$date);
$new_date = array($date[2], $date[1], $date[0]);
$date = implode("-",$new_date);
$date=date($date_format,strtotime($date));
return $date;
}
Another way if your require to format date as per your requirement:
$date=explode("/","13/02/23");
$new_date_format=date('Y-m-d',strtotime($date[2].'-'.$date[1].'-'.$date[0]));

Categories