I need to get only date from datetime variable. I was using this code. It was working fine but now I am facing problem when I'm trying to store only date in mysql please. Take a look at the code below and please tell me what I'm missing,
$date_time = "11-Dec-13 8:05:44 AM"
From the date I got from user input, I need to save only date in att_date variable.
$arr = explode("/", $date_time);
$arr2 = explode(" ", $arr[2]);
$att_date = $arr2[0].'-'.$arr[0].'-'.$arr[1];
Here is the solution:
$date_time = "11-Dec-13 8:05:44 AM";
$new_date = date("Y-m-d H:i:s",strtotime($date_time));
Even if you just use "Y-m-d" it will work instead of using "Y-m-d H:i:s",please practice below example:
$datetime = '2022-10-11 06:38:02';
$getOnlyDate = date('Y-m-d',strtotime($datetime));
echo $getOnlyDate; // output: 2022-10-11
You can get the date from the datetime variable using the
Date();
function from mysql using php query, it automatically extracts the date from it. You can learn it here.
Related
I want to extract date without time from a single value from array result thaht I got.
I tried using array_slice but it didn't work.
My code..
$dateRows = $this->get('app')->getDates();
$dateRow = json_decode(json_encode($dateRows[0], true));
dump($dateRow[0]);die;
And I got result..
"2014-01-01 00:00:00"
And I want it to return just
"2014-01-01"
Very Simple, just use date_create() on your date & then format it using date_format() as follows -
$date = date_create("2014-01-01 00:00:00");
echo date_format($date,"Y-m-d");
So, in your case, it would be something like -
$date1 = date_create($dateRows[0]);
echo date_format($date1,"Y-m-d");
you can use strtotime function as well for getting formatted data
$a = "2014-01-01 00:00:00";
echo date('Y-m-d', strtotime($a));
This might be very simple, but I still need help with this.
I have this portion of php code:
$END = NULL;
if (isset ($_POST['end'])){
$END = date_format($_POST['end'], "Y-m-d H:i");
}
Where $_POST['end'] is a date and time that I get in format dd-mm-YYYY HH:mm. The problem is, as you can guess, that it doesn't transform my input to the Y-m-d H:i format, it just doesn't do anything. But I've followed what I've seen in another code that does indeed work. What am I doing wrong here?
Ignore the fact that I don't check if the input is well written, I assume that it will be.
This is because date_format accepts an object and not a string. You should use the function date and pass to it's second argument a timestamp.
Use date_create() function to convert string to date and then pass it to date_format() function.
if (isset ($_POST['end'])){
$date = create_date( $_POST['end'] );
$END = date_format($date, "Y-m-d H:i");
}
I've got two date objects sent from a date input and a time input.
I want to generate a DateTime object with the yyyy-mm-dd from the date input and hh-mm from the time input.
Data sent from date input
[search_from_date] => 2015-08-04T23:00:00.000Z
Data sent from time input
[search_from_time] => 1970-01-01T02:02:00.000Z
I need to merge these two dates into:
2015-08-04 02:02
I've been playing around with exploding the string, etc to no avail,
Any help would be appriciated
Cheers
You can create two DateTime objects from your strings, then use the second to set the time on the first.
$arr = ['search_from_date' => '2015-08-04T23:00:00.000Z', 'search_from_time' => '1970-01-01T02:02:00.000Z'];
$date = new DateTime($arr['search_from_date']);
$time = new DateTime($arr['search_from_time']);
$date->setTime($time->format('H'), $time->format('i'), $time->format('s'));
echo $date->format('r');
Here's an eval.in with an example - https://eval.in/424209
you can split on "T" like that :
$search_from_date = "2015-08-04T23:00:00.000Z";
$search_from_time = "1970-01-01T02:02:00.000Z";
$tab_date = explode("T", $search_from_date);
$tab_time = explode("T", $search_from_time);
$date_time = new DateTime("$tab_date[0]T$tab_time[1]");
var_dump($date_time);
Try this,
$search_from_date = '2015-08-04T23:00:00.000Z';
$search_from_time = '1970-01-01T02:02:00.000Z';
$data = explode('T',$search_from_date);
$data1 = explode('T',$search_from_time);
$data1 = explode('.',$data1[1]);
echo $data[0].' '.$data1[0];
$time=new DateTime('1970-01-01T02:02:00.000Z');
$date=new DateTime('2015-08-04T23:00:00.000Z');
$newDateTime= new DateTime();
$newDateTime->setTime($time->format('H'),$time->format('i'),$time->format('s'));
$newDateTime->setDate($date->format('Y'),$date->format('m'),$date->format('d'));
echo $newDateTime->format('Y/m/d H:i'));
exit;
Something like this should work.
$finalDateD creates a date string for just the Year, month and day
and
$finalDateT creates a date string for just the Hours, minutes
This is then concatenated into the variable $finalDate;
$finalDateD = date('Y-m-d', strtotime($search_from_date));
$finalDateT = date('H:i', strtotime($search_from_time));
$finalDate = $finalDateD.' '.$finalDateT;
I have get data from oracle database and date value kept on 27-MAY-09. I need to insert this value to mysql database via PHP. I need to convert date format as 2009-05-27.
Any one know about it please let me know correct php statement for do this.
use date() function
$date = '27-MAY-09';
$newData = date('Y-m-d', strtotime($date));
php fiddle
$date = DateTime::createFromFormat('j-M-y', $inputDate);
$newDate = $date->format('Y-m-d');
PHP 5.3 not earlier.
try this
$date1 = "27-MAY-09";
$data2 = date("Y-m-d",strtotime($date1));
I'm having date 20/12/2001 in this formate . i need to convert in following format 2001/12/20 using php .
$var = explode('/',$date);
$var = array_reverse($var);
$final = implode('/',$var);
Your safest bet
<?php
$input = '20/12/2001';
list($day, $month, $year) = explode('/',$input);
$output= "$year/$month/$day";
echo $output."\n";
Add validation as needed/desired. You input date isn't a known valid date format, so strToTime won't work.
Alternately, you could use mktime to create a date once you had the day, month, and year, and then use date to format it.
If you're getting the date string from somewhere else (as opposed to generating it yourself) and need to reformat it:
$date = '20/12/2001';
preg_replace('!(\d+)/(\d+)/(\d+)!', '$3/$2/$1', $date);
If you need the date for other purposes and are running PHP >= 5.3.0:
$when = DateTime::createFromFormat('d/m/Y', $date);
$when->format('Y/m/d');
// $when can be used for all sorts of things
You will need to manually parse it.
Split/explode text on "/".
Check you have three elements.
Do other basic checks that you have day in [0], month in [1] and year in [2] (that mostly means checking they're numbers and int he correct range)
Put them together again.
$today = date("Y/m/d");
I believe that should work... Someone correct me if I am wrong.
You can use sscanf in order to parse and reorder the parts of the date:
$theDate = '20/12/2001';
$newDate = join(sscanf($theDate, '%3$2s/%2$2s/%1$4s'), '/');
assert($newDate == '2001/12/20');
Or, if you are using PHP 5.3, you can use the DateTime object to do the converting:
$theDate = '20/12/2001';
$date = DateTime::createFromFormat('d/m/Y', $theDate);
$newDate = $date->format('Y/m/d');
assert($newDate == '2001/12/20');
$date = Date::CreateFromFormat('20/12/2001', 'd/m/Y');
$newdate = $date->format('Y/m/d');