PHP 01/01/1970 Issues with Date Fields - php

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 = '';}

Related

How can I add a number of days after an end of the month in PHP?

My goal is to print a dueDate.
The due date formula is end of the month adding the days which is 7 on my example.
How can I make it possible?
My echoed value on my actual result is "Due date: 08/01/1970"
My expected result is "Due date: 07/06/2018"
$invoice_date = "11/05/2018";
$days = 7;
$is_invoice = false;
$date = date("d/m/Y", strtotime($invoice_date));
if ($is_invoice) {
$dueDate = date('d/m/Y', strtotime("+$day $days", strtotime($date)));
} else {
$dueDate = date('t/m/Y', strtotime($date));
$dueDate = date('d/m/Y', strtotime("+$day days", strtotime($date)));
}
echo "Due date: $dueDate";
Thanks in advance for the help
Try with the DateTime class:
$date = DateTime::createFromFormat('d/m/Y', '11/05/2018');
$dueDate = clone $date;
$dueDate->modify('+7 days');
echo 'Date : ' . $date->format('d/m/Y') . "\n";
echo 'Due : ' . $dueDate->format('d/m/Y') . "\n";
Output:
Date : 11/05/2018
Due : 18/05/2018
See it here: https://3v4l.org/BjUSK
Your coding logic is perfect except the undefined variable error
$invoice_date = "11/05/2018";
$day = 7;//in below statements used as day
$is_invoice = false;
$date = date("d/m/Y", strtotime($invoice_date));
if ($is_invoice) {
$dueDate = date('d/m/Y', strtotime("+$day days", strtotime($date)));
} else {
$dueDate = date('t/m/Y', strtotime($date));
$dueDate = date('d/m/Y', strtotime("+$day days", strtotime($date)));
}
echo "Due date: $dueDate";
Undefined variable $day;//Change days to day
I'd suggest you to use DateTime class and related functions:
$invoice_date = "11/05/2018";
$days = 7;
$input = date_create_from_format('d/m/Y', $invoice_date);
$result = $input->add(new DateInterval("P${days}D"));
$dueDate = $result->format('d/m/Y');
echo "Due date: $dueDate";
Output:
Due date: 11/05/2018
End of month + 7 days is how I read that ... correct me if I'm wrong:
$invoice_date= '11/05/2018';
$days= 7;
$dueDate= DateTime::createFromFormat('d/m/Y', $invoice_date);
$dueDate->modify('last day of this month')->modify('+7 days');
echo $dueDate->format('d/m/Y');
Invoice Date 11/5/2018 returns 7/6/2018
$invoice_date = "11-05-2018";
$day = 7;//in below statements used as day
$is_invoice = false;
$date = date("Y/m/d", strtotime($invoice_date));
if ($is_invoice) {
$dueDate = date('d/m/Y', strtotime($date. ' + '.$day.' days'));
} else {
$dueDate = date('Y-m-t', strtotime($date));
$dueDate = date('d/m/Y', strtotime($dueDate. ' + '.$day.' days'));
}
echo "Due date: ".$dueDate;

How to get the start date and end date when the year number and month number are known in php

I have the year number (ex: 2018) and the month number (Ex:04). I want to get the starting date and the ending date for that month. like
2018-04-01 and 2018-04-30
try this
$year = 2018;
$month = 4;
$date_start = date('Y-m-d', strtotime(date($year.'-'.$month).' first day of this month'));
$date_end = date('Y-m-d', strtotime(date($year.'-'.$month).'last day of this month'));
echo $date_start . ' and ' . $date_end;
$months_array = $this->getting_particular_months_dates($start_day_of_year, $end_day_of_year);
$arra1=array();
foreach ($months_array as $K => $v)
{
$year=explode( '-',$v['month'])[1];
$month=explode( '-',$v['month'])[0];
$arra1[]=$this->get_month_dates((int)$year,(int)$month);
}
return $arra1;
public function getting_particular_months_dates($year_start_date, $year_end_date)
{
$month_array = array();
$date1 = $year_start_date;
$date2 = $year_end_date;
$output = [];
$time = strtotime($date1);
$last = date('m-Y', strtotime($date2));
do {
$month = date('m-Y', $time);
$total = date('t', $time);
$output[] = [
'month' => $month,
'total' => $total,
];
$time = strtotime('+1 month', $time);
} while ($month != $last);
$month_array = $output;
return $month_array;
}
public function get_month_dates($year, $month)
{
$date_start = date('Y-m-d', strtotime(date($year . '-' . $month) . ' first day of this month'));
$date_end = date('Y-m-d', strtotime(date($year . '-' . $month) . 'last day of this month'));
$a = array('first_day' => $date_start, 'last_day' => $date_end);
return $a;
}

Error during third saturday date fetching

I want to fetch third Saturday and I am using php function for that, that i know.
But I am getting wrong data while fetching from an error.
Here is my code:
$frmdate = 2015-06-05;
$todate = 2015-08-31;
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date))
{
$custom_day = date("Y-m-d", $date);
$custom_third_sat[] = date('Y-m-d', strtotime('third Saturday "'.$custom_day.'"'));
}
echo "<pre>";
print_r($custom_third_sat);
Where am I wrong?
Every Months contain only one "third saturday" , so no need to do more looping of days. Just try this Code Once.
$frmdate = "2015-06-05";
$todate = "2015-08-31";
$custom_third_sat=array();
for ($date = date("Y-m-01", strtotime($frmdate)); $date <= $todate; $date = date("Y-m-01",strtotime($date."+1 Month"))) {
if($date>$todate){
break;
}
$t_date=date('Y-m-d', strtotime($date.' third Saturday'));
if($t_date>=$frmdate && $t_date<=$todate)
{
$custom_third_sat[] = $t_date;
}
}
echo "<pre>";print_r($custom_third_sat);
you should use of like third saturday of:try this
$custom_third_sat[] = date('Y-m-d', strtotime("third saturday of $custom_day"));
your full code can be something like this:
$frmdate = '2015-06-05';
$todate = '2015-08-31';
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date))
{
$custom_day = date("Y-m-d", $date);
if(!isset($custom_third_sat[date('Y-m-d', strtotime("third saturday of $custom_day"))])){
$custom_third_sat[date('Y-m-d', strtotime("third saturday of $custom_day"))] = date('Y-m-d', strtotime("third saturday of $custom_day"));
}
}
echo "<pre>";
print_r($custom_third_sat);
You are just missing the quotes to dates
<?php
$frmdate = '2015-06-05';
$todate = '2015-08-31';
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date))
{
echo"assa";
$custom_day = date("Y-m-d", $date);
$custom_third_sat[] = date('Y-m-d', strtotime('third Saturday "'.$custom_day.'"'));
}
echo "<pre>";
print_r($custom_third_sat);
?>

How to convert the time from AM/PM to 24 hour format in PHP?

For example, I have a time in this format:
eg.
09:15 AM
04:25 PM
11:25 AM
How do I convert it to :
09:15
16:25
23:25
Currently my code is :
$format_time = str_replace(" AM", "", $time, $count);
if ($count === 0){
$format_time = strstr($time, ' PM', true);
$format_time = ......
}
However, it seems there are some easier and more elegant way to do this?
$time = '23:45';
echo date('g:i a', strtotime($time));
How do I fit the above sample in my case? Thanks.
Try with this
echo date("G:i", strtotime($time));
or you can try like this also
echo date("H:i", strtotime("04:25 PM"));
If you use a Datetime format see http://php.net/manual/en/datetime.format.php
You can do this :
$date = new \DateTime();
echo date_format($date, 'Y-m-d H:i:s');
#output: 2012-03-24 17:45:12
echo date_format($date, 'G:ia');
#output: 05:45pm
PHP 5.3+ solution.
$new_time = DateTime::createFromFormat('h:i A', '01:00 PM');
$time_24 = $new_time->format('H:i:s');
Output: 13:00:00
Works great when formatting of date is required. Check This Answer for details.
You can use this for 24 hour to 12 hour:
echo date("h:i", strtotime($time));
And for vice versa:
echo date("H:i", strtotime($time));
We can use Carbon
$time = '09:15 PM';
$s=Carbon::parse($time);
echo $military_time =$s->format('G:i');
http://carbon.nesbot.com/docs/
$Hour1 = "09:00 am";
$Hour = date("H:i", strtotime($Hour1));
$s = '07:05:45PM';
$tarr = explode(':', $s);
if(strpos( $s, 'AM') === false && $tarr[0] !== '12'){
$tarr[0] = $tarr[0] + 12;
}elseif(strpos( $s, 'PM') === false && $tarr[0] == '12'){
$tarr[0] = '00';
}
echo preg_replace("/[^0-9 :]/", '', implode(':', $tarr));
$time = '09:15 AM';
$chunks = explode(':', $time);
if (strpos( $time, 'AM') === false && $chunks[0] !== '12') {
$chunks[0] = $chunks[0] + 12;
} else if (strpos( $time, 'PM') === false && $chunks[0] == '12') {
$chunks[0] = '00';
}
echo preg_replace('/\s[A-Z]+/s', '', implode(':', $chunks));
We Can Create AM/PM by Carbon Laravel
Carbon::parse('your Value')->format('g:i A');

How to get year and month from a date - PHP

How to get year and month from a given date.
e.g. $dateValue = '2012-01-05';
From this date I need to get year as 2012 and month as January.
Use strtotime():
$time=strtotime($dateValue);
$month=date("F",$time);
$year=date("Y",$time);
Using date() and strtotime() from the docs.
$date = "2012-01-05";
$year = date('Y', strtotime($date));
$month = date('F', strtotime($date));
echo $month
Probably not the most efficient code, but here it goes:
$dateElements = explode('-', $dateValue);
$year = $dateElements[0];
echo $year; //2012
switch ($dateElements[1]) {
case '01' : $mo = "January";
break;
case '02' : $mo = "February";
break;
case '03' : $mo = "March";
break;
.
.
.
case '12' : $mo = "December";
break;
}
echo $mo; //January
I'm using these function to get year, month, day from the date
you should put them in a class
public function getYear($pdate) {
$date = DateTime::createFromFormat("Y-m-d", $pdate);
return $date->format("Y");
}
public function getMonth($pdate) {
$date = DateTime::createFromFormat("Y-m-d", $pdate);
return $date->format("m");
}
public function getDay($pdate) {
$date = DateTime::createFromFormat("Y-m-d", $pdate);
return $date->format("d");
}
You can use this code:
$dateValue = strtotime('2012-06-05');
$year = date('Y',$dateValue);
$monthName = date('F',$dateValue);
$monthNo = date('m',$dateValue);
printf("m=[%s], m=[%d], y=[%s]\n", $monthName, $monthNo, $year);
I will share my code:
In your given example date:
$dateValue = '2012-01-05';
It will go like this:
dateName($dateValue);
function dateName($date) {
$result = "";
$convert_date = strtotime($date);
$month = date('F',$convert_date);
$year = date('Y',$convert_date);
$name_day = date('l',$convert_date);
$day = date('j',$convert_date);
$result = $month . " " . $day . ", " . $year . " - " . $name_day;
return $result;
}
and will return a value: January 5, 2012 - Thursday
$dateValue = '2012-01-05';
$yeararray = explode("-", $dateValue);
echo "Year : ". $yeararray[0];
echo "Month : ". date( 'F', mktime(0, 0, 0, $yeararray[1]));
Usiong explode() this can be done.
$dateValue = '2012-01-05';
$year = date('Y',strtotime($dateValue));
$month = date('F',strtotime($dateValue));
I personally prefer using this shortcut. The output will still be the same, but you don't need to store the month and year in separate variables
$dateValue = '2012-01-05';
$formattedValue = date("F Y", strtotime($dateValue));
echo $formattedValue; //Output should be January 2012
A little side note on using this trick, you can use comma's to separate the month and year like so:
$formattedValue = date("F, Y", strtotime($dateValue));
echo $formattedValue //Output should be January, 2012
$dateValue = strtotime($q);
$yr = date("Y", $dateValue) ." ";
$mon = date("m", $dateValue)." ";
$date = date("d", $dateValue);

Categories