How to convert this string
$parent = "2011-08-04 15:00:01";
to separate it into two strings:
$child1= "2011-08-04";
$child2 = "12:00:01";
And then convert
$child1 to this format 8.4.2011
and
$child2 to this format 15:00
$time = new DateTime("2011-08-04 15:00:01");
$date = $time->format('n.j.Y');
$time = $time->format('H:i');
$parent = '2011-08-04 15:00:01';
$timestamp = strtotime($parent);
$child1 = date('n.j.Y', $timestamp); // d.m.YYYY
$child2 = date('H:i', $timestamp); // HH:ss
date('Y-m-d', strtotime( '2015-04-16 15:00:01' ) );
this will give you correct date format followed by mysql
i.e 2015-04-16 03:54:17
Related
I trying to format string to date. String looks like that: 20200219 and I want to format it like 2020-02-19. Can some help me with this?
You have multiple options:
Use strtotime() on your first date then date('Y-m-d') to convert it back:
$changed_date = "20200219";
echo date("Y-m-d", strtotime($changed_date ) );
$time = strtotime('03/05/2020');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2020-03-05
You need to be careful with m/d/Y and m-d-Y formats. PHP considers / to mean m/d/Y and - to mean d-m-Y. I would explicitly describe the input format in this case:
$ymd = DateTime::createFromFormat('m-d-Y', '03/05/2020')->format('Y-m-d');
Another Option:
$d = new DateTime('03/05/2020');
$timestamp = $d->getTimestamp(); // Unix timestamp
$formatted_date = $d->format('Y-m-d'); // 2020-03-05
you can do like that
$s = '20200219';
$date = strtotime($s);
echo date('Y-m-d', $date);
if it's a string you could do like this:
$date=date_create("20200219");
return date_format($date,"Y-m-d");
Hope it helped.
Try this one:-
$var = "20200219";
echo date("Y-m-d", strtotime($var) );
You can try php way:
date("Y-m-d", strtotime("20200219") );
How can I reformat/split the datetime data (from a database) into two variables, date and time, and then add them back to the array?
At present $datetime contains data such as: 2014-12-03 00:00:00
I want to split it out into $date and $time
I have the following, but when I echo out the $date and $time vars they are empty:
$datetime = date($data['date_time']);
$date = date_format($datetime,"Y-m-d'");
$time = date_format($datetime,"H:i");
DateTime objects are created using date_create(), not date()
$datetime = date_create($data['date_time']);
$date = date_format($datetime,"Y-m-d");
$time = date_format($datetime,"H:i");
Why not simply do it using DateTime, in OOP way
$date = new DateTime('2000-12-31 00:00:00');
$dateonly=$date->format('Y-m-d');
$timeonly=$date->format('H:i');
DEMO
With your current code you can try this :
$datetime = date($data['date_time']);
$dateArray = explode($datetime, " ");
$date = $dateArray[0];
$time = $dateArray[1];
I would do:
$datetime = new DateTime($data['date_time']);
$date = $datetime->format('Y-m-d');
$time = $datetime->format('H:i:s');
You can use strtotime:
$datetime = date($data['date_time']);
$date = date("Y-m-d",strtotime($datetime));
$time = date("H:i:s",strtotime($datetime));
I am using this code to add a week to a date:
$date1 = "2009-10-11";
$d = new DateTime($date1);
$d->modify( '+1 week' );
echo $d->format( 'Y m d' ), "\n";
It works fine good but want to add this functionality:
$startDate = "2009-10-11";
$endDate = "2010-01-20";
And want to create an array that holds ALL the +1 weeks IN BETWEEN these dates. How can i do this?
Here is one way of doing it:
$startDate = "2009-10-11";
$endDate = "2010-01-20";
$dates = array();
$temp = strtotime($startDate);
do {
$dates[] = date("Y-m-d", $temp);
$temp = strtotime("+1 week", $temp);
} while ($temp < strtotime($endDate));
print_r($dates);
You can see a demo here
Dates can be converted to timestamps. Timestamps are great for being compared because they basically just integers.
What I would do as a quick'n'dirty solution is to convert both your dates to timestamps and then design a loop like this (pseudo-code) :
timestamp = start_timestamp
WHILE timestamp < end_timestamp
timestamp = timestamp + 1 week
dates[] = timestamp
END WHILE
I have two inputs, one for the date in yyyy/mm/dd format and another for time in 12:15 AM. Now I need to save into the the databse a timestamp. I get both inputs lets say:
$my_date = '2013/12/22';
$my_time = '12:50 PM';
How do I get a timestamp to save into db?
Thank you
Give this a try...
$my_date = '2013/12/22';
$my_time = '12:50 PM';
$full_date = $my_date . ' ' . $my_time;
$timestamp = strtotime($full_date);
Use DateTime:createFromFormat()
<?php
$my_date = '2013/12/22';
$my_time = '12:50 PM';
$d = sprintf('%s %s', $my_date, $my_time);
$dt = DateTime::createFromFormat('Y/m/d h:i A', $d);
$ts = $dt->getTimestamp();
var_dump($ts);
Yields:
int 1387716600
Hope this helps :)
Edit
PHP's date format reference
You could use strtotime() to convert it into an UNIX timestamp.
E.g.:
$my_date = strtotime('2013/12/22');
$my_time = strtotime('12:50 PM');
Full date + time timestamp
$timestamp = strtotime('2013/12/22 12:50 PM');
More info: http://php.net/manual/en/function.strtotime.php
In my database I have a time stamp column...which reflects a format like this: 2012-04-02 02:57:54
However I would like to separate them up into $date and $time.
After some research through the php manual...I found that date(), date_format() and strtotime() are able to help me to separate them...(not sure if I am right)
But I am not very sure of how to code it out...
In my php file...the timestamp extracted would be $row['DATETIMEAPP'].
Will
$date = strtotime('d-m-Y',$row['DATETIMEAPP']);
$time = strtotime('Gi.s',$row['DATETIMEAPP']);
or
$date = date('d-m-Y',$row['DATETIMEAPP']);
work?
Can I use date() to get the time as well??
Thanks in advance
$timestamp = strtotime($row['DATETIMEAPP']);
gives you timestamp, which then you can use date to format:
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
Alternatively
list($date, $time) = explode('|', date('d-m-Y|Gi.s', $timestamp));
If you dont want to change the format of date and time from the timestamp, you can use the explode function in php
$timestamp = "2012-04-02 02:57:54"
$datetime = explode(" ",$timestamp);
$date = $datetime[0];
$time = $datetime[1];
$mydatetime = "2012-04-02 02:57:54";
$datetimearray = explode(" ", $mydatetime);
$date = $datetimearray[0];
$time = $datetimearray[1];
$reformatted_date = date('d-m-Y',strtotime($date));
$reformatted_time = date('Gi.s',strtotime($time));
You can try this:
For Date:
$date = new DateTime($from_date);
$date = $date->format('d-m-Y');
For Time:
$time = new DateTime($from_date);
$time = $time->format('H:i:s');
$timestamp='2014-11-21 16:38:00';
list($date,$time)=explode(' ',$timestamp);
// just time
preg_match("/ (\d\d:\d\d):\d\d$/",$timestamp,$match);
echo "\n<br>".$match[1];
Works for me:
select DATE( FROM_UNIXTIME( columnname ) ) from tablename;
If you want to use the DateTime class, you can do so like this:
$timestamp = $row['DATETIMEAPP']; // String formatted as "2012-04-02 02:57:54"
// Create DateTime object from custom timestamp
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $timestamp);
$date = $dt->format('d-m-Y'); // String variable of just the date
$time = $dt->format('H:i:s'); // String variable of just the time
And if you're concerned about using DateTime over strtotime() or date(), I'd like to point you in the direction of this conversation on StackOverflow titled "DateTime class vs. native PHP date-functions."
Optionally you can use database function for date/time formatting. For example in MySQL query use:
SELECT DATE_FORMAT(DATETIMEAPP,'%d-%m-%Y') AS date, DATE_FORMT(DATETIMEAPP,'%H:%i:%s') AS time FROM yourtable
I think that over databases provides solutions for date formatting too