I want to convert negative timestamp value to human readable value. This is my code:
$timestamp = -30607469951;
$date = new DateTime();
$date->setTimestamp($timestamp);
$date->format('j/n/Y');
All i did was echo out the formatted $date, is that what you wanted?
$timestamp = -30607469951;
$date = new DateTime();
$date->setTimestamp($timestamp);
echo $date->format('Y-m-d');
Related
Sorry but I still don't know how to write a good title for this question.
In database, I have a string of timezone, ex: "Asia/Bangkok" and I want to convert it to "+07:00". How I can do it?
Here is my code:
$newTZ = new DateTimeZone("Asia/Bangkok");
But I don't know what is next. Thanks you so much.
Simply set the timezone to a DateTime instance and display using the "P" format
$newTZ = new DateTimeZone("Asia/Bangkok");
echo (new DateTime('now', $newTZ))->format('P'); // displays "+07:00" for 'now'
https://3v4l.org/0Cb0C
//GMT
$time = '01/01/2018 12:00 AM';
$zone = 'Asia/Bangkok';
$schedule_date = new DateTime($time, new DateTimeZone($zone) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$time2 = $schedule_date->format('Y-m-d H:i:s');
//Time conveted to TimeZone
echo $time2;
//Compare time between two dates
$date1=date_create($time);
$date2=date_create($time2);
echo "<pre>";
$object = date_diff($date1, $date2);
//Detailed object
print_r($object);
//Get whatever format you want.
echo $object->h . ':' . $object->i;
Try to do:
$datetime = new DateTime();
$onehour = new DateInterval('PT1H');
$datetime->add($onehour);
$timestamp = $datetime->date;
echo $timestamp; // nothing
What's wrong with it?
There's no such date property in the DateTime object.
Instead you can use format() to get the date formatted according to given format.
echo $datetime->format('Y-m-d H:i:s');
or use getTimestamp() to get the Unix timestamp:
echo $datetime->getTimestamp();
http://php.net/manual/en/datetime.format.php
http://php.net/manual/en/datetime.gettimestamp.php
You have to use :
DateInterval::createFromDateString("1 hours");
Instead of :
new DateInterval('PT1H');
As Given Below Code :
$onehour = DateInterval::createFromDateString("1 hours");
$date = new DateTime();
$date->add($onehour);
echo $date->format("Y-m-d H:i:s");
I am trying to increment 1 month from my given date and I am receiving the following error while converting;
Note: Kindly check the comment in the code.
Catchable fatal error: Object of class DateTime could not be converted
to string
My Code:
$start_date = '25-05-2015'; // 25th of May 2015
$dateVal = explode("-",$start_date);
$newdate = $dateVal[1].'-'.$dateVal[0].'-'.$dateVal[2].' 00:00:00'; // Converted
$cal_date = DateTime::createFromFormat('m-d-Y H:i:s', $newdate);
$date = new DateTime($cal_date); // To give an standard format for this input only all the above date calculations are made. Pls specify any useful method?
$interval = new DateInterval('P1M');
$date->add($interval);
$currentDate = $date->format('Y-m-d');
How can I convert the date to find the date after one month?
Just reuse your $cal_date, no need to feed a new $date object into it:
$start_date = '25-05-2015'; // 25th of May 2015
$dateVal = explode("-",$start_date);
$newdate = $dateVal[1].'-'.$dateVal[0].'-'.$dateVal[2].' 00:00:00'; // Converted
$cal_date = DateTime::createFromFormat('m-d-Y H:i:s', $newdate);
$interval = new DateInterval('P1M');
$cal_date->add($interval);
$currentDate = $cal_date->format('Y-m-d');
echo $currentDate;
The problem is, you're trying to feed a DateTime object instead of a string. It already says it in the error:
$cal_date = DateTime::createFromFormat('m-d-Y H:i:s', $newdate);
// ^ date time object
$date = new DateTime($cal_date); // feeding the DateTime Object into the constructor
Or why not just directly set the format in place, remove that part wherein you're exploding. You don't actually need those:
$start_date = '25-05-2015';
$cal_date = DateTime::createFromFormat('d-m-Y H:i:s', $start_date . ' 00:00:00');
$interval = new DateInterval('P1M');
$cal_date->add($interval);
$currentDate = $cal_date->format('Y-m-d');
echo $currentDate;
Date Format:
$date1 = '16-MAR-2015';
$date2 = '04-FEB-15';
How can I check if
$date1 <= $date2 || $date1 => $date2
Do I need to convert date format in
$date1 = '16-3-2015';
$date2 = '04-2-15';
Use strtotime():
<?php
$date1 = strtotime("16-MAR-2015");
$date2 = strtotime("04-FEB-15");
?>
and compair
Try this way, it works. DEMO
$date1 = DateTime::createFromFormat('j-M-Y', '16-MAR-2015');
$date2 = DateTime::createFromFormat('j-M-y', '04-FEB-15');
$date1=$date1->format('Y-m-d');
$date2=$date2->format('Y-m-d');
var_dump($date1 <= $date2);
var_dump($date1 >= $date2);
You don't need to convert anything with the given formats, DateTime will do what you need.
Just turn them into DateTime objects and compare them :
$date1 = '16-MAR-2015';
$date2 = '04-FEB-15';
$date1 = date_create($date1);
$date2 = date_create($date2);
// $date1 > $date2 -> true
// $date1 < $date2 -> false
Or
if (date_create($date1) > date_create($date2)) {
// Use $date1 unchanged
}
First, you need to turn them into a DateTime object.
DEMO
$date1 = '16-MAR-2015';
$date2 = '04-FEB-15';
$dateComp1 = new DateTime($date1);
$dateComp2 = new DateTime($date2);
Then you can compare them, DateTime is smart enough to convert the format automatically.
$myDateTime = DateTime::createFromFormat('Y-m-d', $dateString); $newDateString = $myDateTime->format('m/d/Y'); There is a magical function called strtotime()
If you need to print the value in a readable format you can use the function DATE
date("d-m-Y h:i:s",$date1);
In your case.
You want to use
Date.parse()
try
date1= new DateTime();
date1= new Datetime->format('d-m-Y'); // this will print year 4 digits ie; 2017
date2= new DateTime();
date2= new DateTime->format('d-m-y'); //this will print 2 digit year ie; 17
// to compare
$differance=$date1->diff($date2);
echo $differance->format('d-m-y'); //or change to upper case Y for 4 digit year
note... in mysql 5.6 it seems the input format must be Y first: if you insert it with Y-first format first, it seems to compare correctly no matter what the second format is.
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));